How do you divide 1.6 / 0.1

Started by
19 comments, last by GutenTag 8 years, 7 months ago

Hi all.

I know dumb ass question.

I'm using vc10 express.

if I do this

float f = 1.6 / 0.1 = 16; all good

But if I do this

double mass = 0.1;

float posx = 1.6;

double dd = posx / mass;

dd = 1.6;

can 0.1 ever = 0.0, as for now its = to 1.0

windows caluclator saying its 16 so does the Iphone vc saying its 1.6

Advertisement
Use two equal signs == instead of one = when you want to make a comparison. A single equal sign always means "assign", which will overwrite the variable's value with a new value.

Also, you might have problems comparing floating point values to exact numbers, especially in the case of 0.1 and 1.6 due to the inexact internal format of floats and doubles.

no I looking at the value from setting a watch. I know that about floats but this is no rounding error there must be some thing up. its turning 0.1 into 1.0.

even Like this.

.


double e = 1.6 / 0.1;
//works
			double m = RTSComponentMotion.m_dMass;
			double x = m_vSteeringForce.x;
			double y = m_vSteeringForce.y;
			double z = m_vSteeringForce.z;

			x = x / m;
			y = y / m;
			z = z / m;
//does not work
			//Acceleration = Force/Mass
			D3DXVECTOR3 acceleration;
			acceleration.x = x;
			acceleration.y = y;  
			acceleration.z = z;

did some thing break.

Can you give some more detail on "does not work". ? What you wrote should not be off by a factor of ten.

On the inexact and rounding thing, that usually takes place about six decimal digits out.

People are used to base ten. So we are fine with 0.1 or 0.2 or 0.9321 and similar. That 0.9321 is represented as 9/10 + 3/100 + 2/1000 + 1/10000.

But in base ten we get really messed up when trying to represent a fraction like 2/3. We write 0.666666, repeating forever because we cannot exactly represent it. No matter how hard we try in base ten, it will always be an inexact representation of 2/3.

The same thing applies in other number bases.

Computers are base two. They do fractions as 1/2, 1/4, 1/8, 1/16, 1/32, 1/64. They are combined the same way we add 1/10, 1/100, 1/1000, 1/10000.

Trying to represent 0.6 will never work out exactly in base two. As a fractional binary is 0.1001 1001 1001 1001... that is, 1/2 + 1/16 + 1/32 + 1/256 + 1/512 + 1/4096, which Google says is 0.59985351562. It gets really close to 0.6, just like 0.66666 in base ten gets really close to 2/3.

Whenever you work with floating point numbers, it is best to think of them as approximations. They are usually right within a small tolerance. There are technical rules for figuring out the precision, but they're probably more than you want in this post.

Since they're approximations, and since only a limited number of bits fit in a value, any time you mix floating points that are different orders of magnitude, about six digits different, the math tends to break down quickly. So if you've got very small values of acceleration combined with very large forces, the math can break down when the processor tries to convert them to the same scale for the operations.

I think that it is not a matter of rounding, I guess that you use a variable (mass) which got either not properly initiliased or overwritten. Output the variable just before doing the calculation to check it.

long double seems to give the correct results.

long double m = RTSComponentMotion.m_dMass;

long double x = m_vSteeringForce.x;

long double y = m_vSteeringForce.y;

long double z = m_vSteeringForce.z;

x = x / m;

y = y / m;

z = z / m;

but why does this work;

float x = 1.6 / 0.1; Does the compiler use long doubles.???

What are the actual values on each of those lines?

Stop it in the debugger.

From the debugger screen, what are the three actual values of m_vSteeringForce.x,y,z? What is the actual value of m? What is the value that is stored?

/Edit: I can think of many possibilities, including that x,y, and z are integers rather than floating point types.

I'm viewing it all in the debugger watch window.

D3DXVECTOR3 m_vSteeringForce;

m_vSteeringForce.x = -2.6226043701171875e-005;

m_vSteeringForce.y = -6.1118044e-010;

m_vSteeringForce.z = 2.5629997e-006;

long double m = 0.1;//mass

long double x = m_vSteeringForce.x;

long double y = m_vSteeringForce.y;

long double z = m_vSteeringForce.z;

x = x / m;

y = y / m;

z = z / m;

x = -2.6226043701171875e-005;

y = -6.1118043959140778e-009;

z = 2.5629997253417969e-005;


double m = RTSComponentMotion.m_dMass;

That looks suspiciously like a static field to me, which has a few ways of being misused that can cause mysterious problems. Can you post the definitions of RTSComponentMotion and m_dMass, the name of the file they're in, and the code assigning their values and the name of the file(s) those are in?

m_vSteeringForce.x = -2.6226043701171875e-005;

(later)

x = -2.6226043701171875e-005;



m_vSteeringForce.y = -6.1118044e-010;
m_vSteeringForce.z = 2.5629997e-006;

(later)

y = -6.1118043959140778e-009;
z = 2.5629997253417969e-005;


Conclusion: Your computer is haunted.

This topic is closed to new replies.

Advertisement