Single Vs Int32

I’m porting some Java code where most of the values are declared a type float. I think Java floats are single-precision 32-bit IEEE 754 floating point numbers. Up until now I’ve been using the Xojo Double type in the port but since I am targeting both 32-bit and 64-bit apps, I’m guessing that wrong?

What is the equivalent data type for single-precision 32-bit IEEE 754 floating point in Xojo? Is it Int32 or Single?

Thanks

Well it certainly isn’t Int32 (or any other datatype that contains the letters “INT”)… those are INTEGER datatypes

and to target both 32-bit and 64-bit … you can use CGFloat (read the LR)

Use double.
The only time this would bite you is if dealing with a structure, or reading from a binary file, where you really need to know exactly what is in it… your code cannot afford to mutate because YOU are using 64bit.
It would have to use variables to match what the file was created with.

Pretty sure you want Single
It’s a 32 bit floatng point value ( see http://developer.xojo.com/single)
Doubles are 64 bit (see http://developer.xojo.com/double)

https://en.wikipedia.org/wiki/IEEE_754

Looking through the Java code, there are only a couple of places where (from a technical point) there may be some importance in using 32 vs 64 bits for the value (bit shift operations). That aside, are Singles “faster” or more performant than Doubles? Just wondering if there is any advantage to changing types given the Xojo language reference suggests using Doubles rather than Singles.

If there is a “speed” difference between Single and Double it would be so small you would be hard pressed to quantify it…
Both are IEEE formats, and therefore need to be “encoded” and “decoded” from the storage format to the display format and vice-versa. Single has a smaller footprint 4 bytes vs 8 bytes and therefore less precision (read the LR for details).

Right now in Xojo, a SINGLE is always a SINGLE, a DOUBLE is always a DOUBLE, but a CGFLOAT is SINGLE in a 32bit build and a DOUBLE in a 64bit build…

So if you are just looking for Floating Point variables use DOUBLE
If you have a Declare (or outside format) that REQUIRES a SINGLE (regardless of build mode) then use SINGLE
if you have a Declare (or outside format) that REQUIRES a DOUBLE (regardless of build mode) then use DOUBLE’
if you have a Declare (or outside format) that REQUIRES a SINGLE in 32bit builds and a DOUBLE in 64bit builds then use CGFLOAT

meaning that 99.99% of the time a DOUBLE will work just fine (for Floating point values)

Thanks for clarifying Dave.

Most machines have IEEE both formats in hardware so you’re unlikely to ever notice a difference
And all the hardware we support does

Does anyone bit-shift a floating point value? I’d expect that to be integers only

Personally I wouldn’t recommend the CGFloat for anything except Apple API declares that use CGFloat. You should ALWAYS double check the documentation for this as there are plenty of APIs that still use singles under 64-Bit.

The number pretty much suggests that a Single is what you’re looking for, because it says “32-Bit” (4 bytes) and floating point (also known as a float in other languages).

A single is 4 bytes and a double is 8 bytes.

Bit late to the conversation, here’s something I put together at the start of the year:

http://blog./2017/01/22/xojo-data-types/

1 Like