![]() |
Forum Index : Microcontroller and PC projects : Sorry for the stupid math question
Author | Message | ||||
RCMAN Newbie ![]() Joined: 31/10/2020 Location: CanadaPosts: 29 |
If I have a float value how do I get the value and remainder separately? Thanks RC |
||||
twofingers![]() Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1593 |
Hi RC, try FIX(number) Michael causality ≠correlation ≠coincidence |
||||
CaptainBoing![]() Guru ![]() Joined: 07/09/2016 Location: United KingdomPosts: 2170 |
x= 3.141 y=Int(x)' integer part z=x-y ' fractional part |
||||
RCMAN Newbie ![]() Joined: 31/10/2020 Location: CanadaPosts: 29 |
Thank you for both your answers. RC |
||||
RCMAN Newbie ![]() Joined: 31/10/2020 Location: CanadaPosts: 29 |
![]() Working Perfect! Edited 2020-12-11 02:50 by RCMAN |
||||
Bizzie Senior Member ![]() Joined: 06/07/2014 Location: AustraliaPosts: 192 |
Why not use MOD? eg mod(3.141,1) = .141 Rob White |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 420 |
I wonder which way is less computationally complex: Int(x) and Mod(x,1) OR Int(x) and calculate (x-int) Any other ways? Nim Entropy is not what it used to be |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 420 |
Int(x) and Mod(x,1) OR Int(x) and calculate (x-int) Any other ways? Nim I rejected making into a string and splitting up and then making back into numbers. Entropy is not what it used to be |
||||
toml_12953 Guru ![]() Joined: 13/02/2015 Location: United StatesPosts: 442 |
Int(x) and Mod(x,1) OR Int(x) and calculate (x-int) Any other ways? Nim I don't know about MMBASIC but I do know that in some BASICs, the subtraction method sometimes has a rounding error. In ANSI/ISO BASIC, there's an FP() function to get the fractional part that eliminates errors. |
||||
toml_12953 Guru ![]() Joined: 13/02/2015 Location: United StatesPosts: 442 |
eg mod(3.141,1) = .141 Clever! |
||||
twofingers![]() Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1593 |
I don't! ![]() >Print field§("3.14159",1,".") 3 >Print field§("3.14159",2,".") 14159 not soo bad! ![]() causality ≠correlation ≠coincidence |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Have you tried? The format for MOD would be 3.141 MOD 1 in MMBasic and MOD seems to only works on integers so will always give zero. It would be worth checking the difference between CINT, INT and FIX if you are likely to have negative numbers. Anther can of worms. PRINT "N",,"INT(N)",,"CINT(N)",,"FIX(N)" FOR n = -2 TO 2 STEP 0.25 PRINT n,,INT(n),,CINT(n),,FIX(n) NEXT n Jim VK7JH MMedit |
||||
RCMAN Newbie ![]() Joined: 31/10/2020 Location: CanadaPosts: 29 |
I don't! ![]() >Print field§("3.14159",1,".") 3 >Print field§("3.14159",2,".") 14159 not soo bad! ![]() I was looking for a modulus command used in C RC |
||||
vegipete![]() Guru ![]() Joined: 29/01/2013 Location: CanadaPosts: 1132 |
INT reduces the number by the fractional part - potentially problematic for negative numbers. CINT rounds to the nearest integer - up or down. FIX just hacks off the fractional part - the positive or negative value gets closer to zero. Take your pick... Visit Vegipete's *Mite Library for cool programs. |
||||
twofingers![]() Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1593 |
@RC something like that: function fract(a as float) as float fract=a-FIX(a) end function causality ≠correlation ≠coincidence |
||||
RCMAN Newbie ![]() Joined: 31/10/2020 Location: CanadaPosts: 29 |
something like that: function fract(a as float) as float fract=a-FIX(a) end function I was working out row and column and CaptainBoing example helped me solve that problem. It's all good now. One thing out of the way, moving on to animation and background detection. RC |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |