php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #9411 Arithmetic error
Submitted: 2001-02-22 15:45 UTC Modified: 2001-02-22 16:07 UTC
From: robburne at hotmail dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 4.0.4pl1 OS: Windows 98
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: robburne at hotmail dot com
New email:
PHP Version: OS:

 

 [2001-02-22 15:45 UTC] robburne at hotmail dot com
Hi I am not sure if I have found a bug - what do you think.......?

Consider this:

$var1 = -0.32 + (0.1*3.2)

This should give var1 the value 0, instead it gives

5.5511151231258E-17 

Interestingly if you increase -0.33 and 3.3 respectively it gives the correct answer. Werid!

Rob Burne

robburne@hotmail.com

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-02-22 16:05 UTC] derick@php.net
This is normal floating point math behavior
 [2001-02-22 16:07 UTC] lyric@php.net
This is nothing to do with PHP, its just part of the way computers store floating-point numbers. It stores an aproximation of 0.32, as close as it can get, but when you start multiplying it, the little difference between 0.32 and what it stored starts to get magnified.

BTW, 5.5511151231258E-17 is 0.000000000000000055511151231258 which is *really* closed to 0.

If you want to output formatted numbers, you should either round it, or use printf()
http://www.php.net/manual/en/function.printf.php

 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Tue Nov 12 08:01:35 2019 UTC