Igor Minascurta
Igor Minascurta

Reputation: 75

Arithmetic operations with php variables

I have a table that displays an arithmetic operation with values from database. In database all values are FLOAT type. My arithmetic operation is:

        $difference = $row['variable1']+$row['variable2']-$row['variable3']-$row['variable4'];
<td><?php echo (round($difference, 2)) ?></td>

So. my problem is the the calculation of some values that i insert. For example:

variable1=10      
variable2=10.08   
variable3=10   
variable4=10.08

Normal it must give difference=0; but it is displaying difference= -0; In every case if i use values with xx.07, xx.08, xx.09 it gives me an result = -0; It doesn't happen wih values up to xx.07.

Example: variable1=10; variable2=10.06; variable3=10;variable4=10.06; The result = 0;
If i don't round the difference it gives a result like this : -5.68434188608E-14;

I tried to round variables before calculation, i tried to use "()" in the calculation, i tried to round variables directly in operation, because i thinked it takes more than 2 values after < ,>(FLOAT type).

I tried to round values when inserting in database, but it gives me error inserting for input fields, because it have more than 4 variables(not used in the difference operation).

Thx for help me in this problem, i really don't understand why it happens like this.

Upvotes: 0

Views: 811

Answers (5)

ro͢binmckenzie
ro͢binmckenzie

Reputation: 325

The problem is that floating point numbers are stored in the database as binary representations, and so are sometimes not exactly the same as the decimal.

You can start to understand this by thinking about how the decimal part might be stored:

0.5 (dec)   is 1/2 which in binary as 0.1 (bin)
0.25 (dec)  is 1/4 which in binary as 0.01 (bin)
0.125 (dec) is 1/8 which in binary as 0.001 (bin)

However, to make 0.08, you have to find a combination of 1/2, 1/4, 1/8, 1/16, etc. which represents this. Unfortunately, this cannot be represented exactly in binary, so it gets close, but not exact.

When calculations are done with these, because they're not exact, there can remain very small differences. The result you saw, "-0" (negative zero) represents a very small negative number, as @DannyPhantom mentioned.

If you're only going to use at most 2 decimal places, you could (depending on your database) use the "decimal" type, which is designed for this.

This online calculator will help you see this: http://www.binaryconvert.com/result_float.html?decimal=049048046048056

For some more information, see this SO question: How to convert float number to Binary?

Upvotes: 0

user3535945
user3535945

Reputation: 241

Try this

$difference = ($row['variable1']+$row['variable2'])-($row['variable3']+$row['variable4']);

Upvotes: 0

Abdulla Nilam
Abdulla Nilam

Reputation: 38672

Note: PHP doesn't handle strings like "12,300.2" correctly by default. See converting from strings.

image

Read about php round() function

Upvotes: 0

Mr Office
Mr Office

Reputation: 290

You would need to round each value before doing calculations on it. This is because of the way PHP stores these numbers internally.

Upvotes: 0

DannyPhantom
DannyPhantom

Reputation: 1063

That is "normal" behavior. You can read about signed zero here. Now about your problem: adding 0 should help.

<td><?php echo ((round($difference, 2))+0) ?></td>

Upvotes: 1

Related Questions