PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Variable Initialization and floats

Ok heres the deal i was writing another simple program to calculate perimeter and i kept getting this runtim error that was "float not loaded" so i set set all the floats to 0.0 during initilazation. I was wondering what made that happen and why setting the floats to 0 stop it, also is it just a good thing to set variables as soon as you declare them if so should you even do it with like char arrays your planning on storing an inpuuted string in?

Second i get this warning "conversion from 'double' to 'float', possible loss of data" when i use this statement

Code:

fltPerimeter = 2.0 * (fltWidth + fltHeight);


BUT if i use this line of code it works with no warnings, whats going on here?

Code:

fltPerimeter = 2 * (fltWidth + fltHeight);
The first number encountered determine the type used for the operations:

In your first case, 2.0 is a double. So all the equation is evaluated using doubles. When the result is assigned to fltPerimeter, you are loosing precision (hence the warning).

In your second case, you are doing all the maths with integers since the "2" is an integer. The result fits without problem in a float, so no warning.

What you really want is:

Code:

fltPerimeter = 2.0f * (fltWidth + fltHeight);