Integer math using floats

Blitz3D Forums/Blitz3D Programming/Integer math using floats

WolRon(Posted 2004) [#1]
Does anyone know for a fact if the following will be a problem?

I have a multidimensional array P(35, 7) that stores some integer numbers in some subsets (3, 4, 7) and some floating point numbers in the other subsets (1, 2, 5, 6).

For specific reasons, I do not wish to use seperate arrays nor Types, so don't suggest to use them please. :)

Because an array can only be declared as integer P(35, 7) or float P#(35, 7), I can't actually have integers in subsets 3, 4, and 7.

So what I want to know is if I create a float array P#(35, 7) and if I perform only 'integer math' on subsets 3, 4, and 7, is there a possibility that the numbers in those subsets could become non-integers do to floating point inaccuracies?

I know you're asking yourself what I mean by 'integer math', so I will explain. For the subsets in question, I will only ever add or subtract exact integer numbers to/from them (i.e. 2.0 + 5.0, or 134.0 - 17.0).

I may even have to perform multiplication (i.e. 32.0 * 7.0).
I probably won't be able however to perform division since that could lead to decimal parts (unless I know in advance that it would not).


So, in other words, if a float variable is set to an integer value (i.e. 4.0), will the zero part of that float always remain zero? Or is there a possibility that it could become non-zero (i.e 4.0000000000001) due to floating point inaccuracies?

The reason I am concerned is because I want to perform checks on the 'integer' subsets 3, 4, and 7 to see if they equal certain values (i.e. If P#(12, 3) = 20 Then ...).
I am afraid that due to floating point inaccuracies, that the checks could fail (i.e. P#(12, 3) = 19.99999999999).

So, does anyone know if I will encounter problems if I perform addition or subtraction on the floats?

Will I encounter problems if I perform multiplication on the floats?

What about performing division?


Difference(Posted 2004) [#2]
You will encounter the problems. You can avoid most by cashing the floats to interger variables when you do the calculations, and the send the result back as a float.

The If P#(12, 3) = 20 is not safe and never will be.

I would avoid doing what you are describing.

Can't you use intergers as floats instead?
I have floats I read from a file as a string, and before using them, I convert *the string* to an interger (well to a string without commas, but you get the drift :) ) . Same way when I write them.


Floyd(Posted 2004) [#3]
Using floats for integers will give exact results if the integers are small enough.

There are 24 bits available in a float, so the biggest integer is %111111111111111111111111, which is 16777215.
The sign is stored separately, so -16777215 is also available.

Addition, subtraction and multiplication are also exact, provided the results fit into 24 bits.

Division will work if the division of integers is exact. For example 6.0/3.0 will be the exact value 2.0.


Sunteam Software(Posted 2004) [#4]
Or you could do:

If Int(P#(12,3))=20



Difference(Posted 2004) [#5]
Unlike me, Floyd knows what he's talking about, so listen to him. I still wouldn't do it though ;)


WolRon(Posted 2004) [#6]
Thanks guys, especially Floyd.

I think I may do what Sunteam suggested though since the Int command in Blitz rounds up or down (Unlike Int command in other Basic languages).


WolRon(Posted 2004) [#7]
Doing some tests, I see that Floyd appears to be correct.
This code succeeds:
For biter = 1 To 100
  For iter = 1 To 16000000
  	a# = a# + 1.0
  Next
  For iter = 2 To 16000000
  	a# = a# - 1.0
  Next
Next
Print a#
WaitKey()
End

and returns the result 100.0

This code fails:
For iter = 1 To 17000000
	a# = a# + 1.0
Next
Print a#
WaitKey()
End

and returns the result 16777200.0