gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > yeah....

#12495 - IAMTHEEVILBEAN - Sun Nov 16, 2003 3:27 am

I know im a n00b...but I never fully understood

float

what is it used for and what does it mean?

thanks
_________________
Moose

#12504 - tepples - Sun Nov 16, 2003 5:44 am

Floating-point arithmetic is like scientific notation. It represents numbers as a given "mantissa" of a given precision times a given power of two. Thus, it can represent both very small numbers and very large numbers. Programs in the C language use the 'float' and 'double' data types to hold floating-point numbers.

However, doing floating-point arithmetic on a machine not specifically designed to handle it is very slow. The 486DX and more powerful x86 processors have it; older x86 CPUs and the GBA's ARM7TDMI don't and must perform the operations in software. On machines without dedicated floating-point support, fixed-point arithmetic is much faster than floating-point arithmetic.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#12517 - IAMTHEEVILBEAN - Sun Nov 16, 2003 3:52 pm

So......you can just use an int instead of it....

I dont know whenever I see float I freak out and get all confused for some reason
_________________
Moose

#12622 - marcmccann - Wed Nov 19, 2003 8:35 pm

Why dont u sit and read a tutorial rather than keep asking daft questions like that?

Not that i have a problem with it but its gotta be embarassing asking questions like that!
_________________
The GBA is one Lean and Mean machine!

#12623 - yaustar - Wed Nov 19, 2003 9:12 pm

to put it in a nutshell, a float variable is a value that can go into decimal (gba has to emulate in software so it is very slow and fixed point maths comes into play), an int variable is 'whole' numbers

examples of float

1.2
302.358
3.0 (note the decimal)

examples of int

1
458
476
3556

more maths

Code:

int var = 5;
var / 2 = 2 (rounds down to the nearest whole number)

float var = 5
var / 2 = 2.5
 

_________________
[Blog] [Portfolio]

#12627 - ampz - Wed Nov 19, 2003 10:27 pm

A Float can represent very large numbers, as well as very small numbers, like:

0.0000000000001234

or

123400000000000000.0

Usually, floats come in handy when you are working with non-linear values of some kind. Exponential stuff... Audio processing is one example where floats are very useful. 3D graphics is another.

However, you must understand that while floats are accurate, float math is not as exact as integer math. The same code can generate slightly different results on different CPU's because of different rounding techniques. When you assign a constant to a float, that constant will be rounded (by the compiler) to the closest float value. For example:

float a = 0.7; //The value actually stored in 'a' will be something very close to 0.7, like 0.699942 or 0.700261

You should *never* use the "equal to" comparison when working with floats.

#12629 - poslundc - Wed Nov 19, 2003 11:36 pm

As tepples pointed out, though, this is all academic anyway since floats are much too slow for most GBA applications anyway. Fixed-point arithmetic only lets you work in relatively close proximity to the decimal point, but this should be more than sufficient for most GBA applications.

ampz wrote:
You should *never* use the "equal to" comparison when working with floats.


Usually if comparison of equality is necessary one should define a tolerance value as some small number, and then check to see whether the absolute value of the difference between the two is smaller than that tolerance value. This only matters if you've been applying mathematical operations to the number, though; the test (0.7 == 0.7) will still return true, even though it's not being represented exactly as 0.7 internally.

Dan.