## Multi line math results different than single line

I was having an issue with some floating point math and I've found that if I do my math on one line, I get -0 passed to tan(), and if I do it across two lines, I get 0 passed to tan(). Have a look:

float theta = PI / 2.f; float p = (PI / 2.f) - theta; float result = tan(p);

The above, p = -0, result = -4.37...

float theta = PI / 2.f; float p = PI / 2.f; p -= theta; float result = tan(p);

The above, p = 0, result = 0.

Can anyone explain the difference? I assume the -0 is causing that result from tan(), although I can't find anything on google that explains why. Why does the exact same calculation spread across different lines result in a different answer?

Thanks

It is probably because of the **type of PI**.

If you use `double`

it will change to float and then the outcome
will be as you just represent.

But if PI is `float`

both of this test scenarios are equal.

**The Century Dictionary: An Encyclopedic Lexicon of the English ,** A unites with several different quantities of B to form definite compounds, these Multiple lines, in ..fort., several lines of detached works or ramparts ary ranged for Multiple point or ent, in math., one which results from the coalescence of two Multi line math results different than single line. point math and I've found that if I do my math on one line, I get -0 passed to tan(), and if I do it across

What @Naor says is probably correct. but I'd like to add something.

You probably not getting `-4.37xx`

but `-4.37xxxe-xx`

which is a pretty small negative number.

Since you can always get errors in floating point math. I'd say there is no need to change your code. Both snips are correct.

**The Century Dictionary: The Century dictionary,** Multiple arc, the system of connecting electric batteries, lamps, or other circuits Multiple lines, in fort., several lines of detached works or ramparts arranged for the Multiple point or nt, in math., one which results from the coalescence of two I was having an issue with some floating point math and I've found that if I do my math on one line, I get -0 passed to tan(), and if I do it across two lines, I get 0 passed to tan().

So this is what, in my opinion, is happening:

In both examples PI is a define, probably defined like this:

#define 3.14 //and some more digits

In C++, number like this is treated as double.

After preprocessing, this expression:

PI / 2.0f

will be treated as double-typed prvalue. This means that this line hides one more operation:

float theta = PI / 2.f;

which is a double-to-float conversion, which definitely looses some precision in this case.

In first example this also happens here:

float p = (PI / 2.f) - theta;

but only after evaluating whole expression. Note that during this evaluation `(PI / 2.f)`

will be still double, but `theta`

will be a float-to-double converted value, which explains the slight difference in result from 0.0.

In your last example you first convert `(PI / 2.f)`

to float:

float p = PI / 2.f;

to subtract float-typed `theta`

from it in next line. Which must result to 0.0, which probably compiler optimized out anyway ; ).

**The Century Dictionary and Cyclopedia: An Encyclopedic Lexicon of ,** Multiple arc, the system of connecting electric batteries, lamps, or other circuits to Multiple lines, in Jort., several lines of detached works or ramparts arranged for Multiple point ort ent, in math., one which results from the coalescence of two Studies have proven that a single-line queue leading to multiple servers is more efficient and results in less variation in the amount of time customers are kept waiting. Still, the single-line queue can appear daunting to customers who fail to understand that one longer line is actually a better bet than taking thei

**The Century Dictionary and Cyclopedia: Dictionary,** Multiple arc, the system of connecting electric batteries, lamps, or other Multiple lines, in fort., several lines of detached works or ramparts arranged for the Multiple point or tangent, in math., one which results from the coalescence of two Attempting to plot one line with multiple colors. Learn more about plot, colors that results in having to either wait, or drawnow which uses a lot of CPU cycles

**Displaymath environment that can align equations and squeezes math,** And the result wouldn't be worthy the trouble, probably: for a single equation, in a display it would create very dissimilar lines next to each other: a line with a Multiple results for equation. multi-line equation in equation environment. 1. Equation number doesn't appear when the equation comes in 2 different page. 4.

**The Mathematics of the Modernist Villa: Architectural Analysis ,** Bill Hillier and Alan Penn's response is that axial line analysis 'deals only with observed of every location relative to every other location and is therefore incapable of one street possesses a variable connection to create differentiated results multiple axial line s be produced in the abstraction process rather than the Python Multi-line Statements Python statements are usually written in a single line. The newline character marks the end of the statement. If the statement is very long, we can explicitly divide into multiple lines with the line continuation character (\).