B4J Question detect if Integer

Blueforcer

Well-Known Member
Licensed User
Longtime User
Hey,

I increase a double by 0.1, 0.2 or 0.5 (depending on the setting) with every tick.
now i want to detect when the double value is an interger e.g 1,2,3,4,5,6 and so on.
how can i do this?

thanks in advise
 
Last edited:

agraham

Expert
Licensed User
Longtime User
It is not advisable to try to do integer checks on Doubles that have been incremented by fractional decimal multiplication as Doubles, being a finite length binary representation of a number, cannot accurately represent decimal fractions so for example (n*10)/n may not result in the naively expected integer value, 10, but may result in something like 10.0000001 or 9.99999. Repeated operations may compound the effect. This is common to all binary computer architectures.

For simplicity I would recommend using Ints or Longs and scaling the multiplier values appropriately.
 
Upvote 0

Blueforcer

Well-Known Member
Licensed User
Longtime User
ok thanks!
Now i changed everything to integer. So i increasing by 1 and check via modulo whether it is divisible by x. Thats working

And yes i checked the addition of the doubles and it is as you said
1591368271805.png
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Except for short loop incrementers, I avoid using Int...
I use a Long, since we are blessed with much more system memory...

This is especially true when one uses a datetime to store as a var declared as int....
Just a hint, I have been bitten too often, spent too much time degugging - now I know better and understand why...
 
Upvote 0
Top