473,322 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

ROUND

Can some one explain if this can be fixed using php.ini settings ?

echo "round 20.545 -".round(20.545,2)."<br>";
echo "round 20.555 -".round(20.555,2)."<br>";
echo "number_format 20.545 -".number_format(20.545, 2, ',',
'.')."<br>";
echo "number_format 20.555 -".number_format(20.555, 2, ',',
'.')."<br>";

PHP Version 4.3.0 / FreeBSD
round 20.545 -20.55
round 20.555 -20.56
number_format 20.545 -20,55
number_format 20.555 -20,55

PHP Version 4.4.4 / CENTOS
round 20.545 -20.55
round 20.555 -20.55
number_format 20.545 -20,55
number_format 20.555 -20,55

PHP Version 5.1.4 / Windows NT
round 20.545 -20.55
round 20.555 -20.56
number_format 20.545 -20,55
number_format 20.555 -20,56

I know that the round always goes for the odd but this is not
consistence with the SO running PHP
Thanks

Dec 21 '06 #1
7 4438
<kk*****@gmail.comwrote in message
news:11**********************@42g2000cwt.googlegro ups.com...
Can some one explain if this can be fixed using php.ini settings ?

echo "round 20.545 -".round(20.545,2)."<br>";
echo "round 20.555 -".round(20.555,2)."<br>";
echo "number_format 20.545 -".number_format(20.545, 2, ',',
'.')."<br>";
echo "number_format 20.555 -".number_format(20.555, 2, ',',
'.')."<br>";

PHP Version 4.3.0 / FreeBSD
round 20.545 -20.55
round 20.555 -20.56
number_format 20.545 -20,55
number_format 20.555 -20,55

PHP Version 4.4.4 / CENTOS
round 20.545 -20.55
round 20.555 -20.55
number_format 20.545 -20,55
number_format 20.555 -20,55

PHP Version 5.1.4 / Windows NT
round 20.545 -20.55
round 20.555 -20.56
number_format 20.545 -20,55
number_format 20.555 -20,56
My only observation about the examples you chose is that the fractional
parts are infinitely repeating "radiximals" in binary.

0.545 = 545/1000 = 109/200 (irreducible)
0.555 = 555/1000 = 111/200 (irreducible)

Both of the numbers above can't be expressed as precise binary floating
point numbers because the denominator after reduction has prime factors that
are not "2".

PHP.INI may be a factor, but it may also be that something at a lower level
is occurring (i.e. machine arithmetic, how certain rounding settings on
floating-point processors or libraries are set).

In other words, you've chosen antagonistic examples.

Why don't you try 7/8 (0.875), 15/16 (0.9275), or 31/32 (0.96875) as the
fractional part of the numbers and see if you can get the same behavior.
I'm guessing that you may not.

Any fraction with a reasonable denomiator (< 2^16) that is a power of 2
would be a reasonable test case.

Note that these are all exactly representable by a typical machine.

Just a guess.

Dec 22 '06 #2
I've tested the same function in the same hardware configuration and
the only difference is the OS, so it must be the libraries the big
problem is that i don't know witch ones to replace in order to get the
system to solve the way i want.


On Dec 22, 4:42 pm, "David T. Ashley" <d...@e3ft.comwrote:
<kkmi...@gmail.comwrote in messagenews:11**********************@42g2000cwt.go oglegroups.com...
Can some one explain if this can be fixed using php.ini settings ?
echo "round 20.545 -".round(20.545,2)."<br>";
echo "round 20.555 -".round(20.555,2)."<br>";
echo "number_format 20.545 -".number_format(20.545, 2, ',',
'.')."<br>";
echo "number_format 20.555 -".number_format(20.555, 2, ',',
'.')."<br>";
PHP Version 4.3.0 / FreeBSD
round 20.545 -20.55
round 20.555 -20.56
number_format 20.545 -20,55
number_format 20.555 -20,55
PHP Version 4.4.4 / CENTOS
round 20.545 -20.55
round 20.555 -20.55
number_format 20.545 -20,55
number_format 20.555 -20,55
PHP Version 5.1.4 / Windows NT
round 20.545 -20.55
round 20.555 -20.56
number_format 20.545 -20,55
number_format 20.555 -20,56My only observation about the examples you chose is that the fractional
parts are infinitely repeating "radiximals" in binary.

0.545 = 545/1000 = 109/200 (irreducible)
0.555 = 555/1000 = 111/200 (irreducible)

Both of the numbers above can't be expressed as precise binary floating
point numbers because the denominator after reduction has prime factors that
are not "2".

PHP.INI may be a factor, but it may also be that something at a lower level
is occurring (i.e. machine arithmetic, how certain rounding settings on
floating-point processors or libraries are set).

In other words, you've chosen antagonistic examples.

Why don't you try 7/8 (0.875), 15/16 (0.9275), or 31/32 (0.96875) as the
fractional part of the numbers and see if you can get the same behavior.
I'm guessing that you may not.

Any fraction with a reasonable denomiator (< 2^16) that is a power of 2
would be a reasonable test case.

Note that these are all exactly representable by a typical machine.

Just a guess.
Dec 26 '06 #3
PHP code
echo "round(10.045,2) - ".round(10.045,2)."<br />";
echo "round(20.045,2) - ".round(20.045,2)."<br />";
echo "round(30.045,2) - ".round(30.045,2)."<br />";
echo "round(40.045,2) - ".round(40.045,2)."<br />";

This is very strange:

round(10.045,2) - 10.04
round(20.045,2) - 20.05
round(30.045,2) - 30.05
round(40.045,2) - 40.05

On Dec 26, 11:57 am, "kkmi...@gmail.com" <kkmi...@gmail.comwrote:
I've tested the same function in the same hardware configuration and
the only difference is the OS, so it must be the libraries the big
problem is that i don't know witch ones to replace in order to get the
system to solve the way i want.

On Dec 22, 4:42 pm, "David T. Ashley" <d...@e3ft.comwrote:
<kkmi...@gmail.comwrote in messagenews:11**********************@42g2000cwt.go oglegroups.com...
Can some one explain if this can be fixed using php.ini settings ?
echo "round 20.545 -".round(20.545,2)."<br>";
echo "round 20.555 -".round(20.555,2)."<br>";
echo "number_format 20.545 -".number_format(20.545, 2, ',',
'.')."<br>";
echo "number_format 20.555 -".number_format(20.555, 2, ',',
'.')."<br>";
PHP Version 4.3.0 / FreeBSD
round 20.545 -20.55
round 20.555 -20.56
number_format 20.545 -20,55
number_format 20.555 -20,55
PHP Version 4.4.4 / CENTOS
round 20.545 -20.55
round 20.555 -20.55
number_format 20.545 -20,55
number_format 20.555 -20,55
PHP Version 5.1.4 / Windows NT
round 20.545 -20.55
round 20.555 -20.56
number_format 20.545 -20,55
number_format 20.555 -20,56My only observation about the examples you chose is that the fractional
parts are infinitely repeating "radiximals" in binary.
0.545 = 545/1000 = 109/200 (irreducible)
0.555 = 555/1000 = 111/200 (irreducible)
Both of the numbers above can't be expressed as precise binary floating
point numbers because the denominator after reduction has prime factors that
are not "2".
PHP.INI may be a factor, but it may also be that something at a lower level
is occurring (i.e. machine arithmetic, how certain rounding settings on
floating-point processors or libraries are set).
In other words, you've chosen antagonistic examples.
Why don't you try 7/8 (0.875), 15/16 (0.9275), or 31/32 (0.96875) as the
fractional part of the numbers and see if you can get the same behavior.
I'm guessing that you may not.
Any fraction with a reasonable denomiator (< 2^16) that is a power of 2
would be a reasonable test case.
Note that these are all exactly representable by a typical machine.
Just a guess.
Dec 26 '06 #4
kk*****@gmail.com wrote:
PHP code
echo "round(10.045,2) - ".round(10.045,2)."<br />";
echo "round(20.045,2) - ".round(20.045,2)."<br />";
echo "round(30.045,2) - ".round(30.045,2)."<br />";
echo "round(40.045,2) - ".round(40.045,2)."<br />";

This is very strange:

round(10.045,2) - 10.04
round(20.045,2) - 20.05
round(30.045,2) - 30.05
round(40.045,2) - 40.05

On Dec 26, 11:57 am, "kkmi...@gmail.com" <kkmi...@gmail.comwrote:
>>I've tested the same function in the same hardware configuration and
the only difference is the OS, so it must be the libraries the big
problem is that i don't know witch ones to replace in order to get the
system to solve the way i want.

On Dec 22, 4:42 pm, "David T. Ashley" <d...@e3ft.comwrote:

>>><kkmi...@gmail.comwrote in messagenews:11**********************@42g2000cwt.go oglegroups.com...
>>>>Can some one explain if this can be fixed using php.ini settings ?
>>>>echo "round 20.545 -".round(20.545,2)."<br>";
echo "round 20.555 -".round(20.555,2)."<br>";
echo "number_format 20.545 -".number_format(20.545, 2, ',',
'.')."<br>";
echo "number_format 20.555 -".number_format(20.555, 2, ',',
'.')."<br>";
>>>>PHP Version 4.3.0 / FreeBSD
round 20.545 -20.55
round 20.555 -20.56
number_format 20.545 -20,55
number_format 20.555 -20,55
>>>>PHP Version 4.4.4 / CENTOS
round 20.545 -20.55
round 20.555 -20.55
number_format 20.545 -20,55
number_format 20.555 -20,55
>>>>PHP Version 5.1.4 / Windows NT
round 20.545 -20.55
round 20.555 -20.56
number_format 20.545 -20,55
number_format 20.555 -20,56My only observation about the examples you chose is that the fractional

parts are infinitely repeating "radiximals" in binary.
>>>0.545 = 545/1000 = 109/200 (irreducible)
0.555 = 555/1000 = 111/200 (irreducible)
>>>Both of the numbers above can't be expressed as precise binary floating
point numbers because the denominator after reduction has prime factors that
are not "2".
>>>PHP.INI may be a factor, but it may also be that something at a lower level
is occurring (i.e. machine arithmetic, how certain rounding settings on
floating-point processors or libraries are set).
>>>In other words, you've chosen antagonistic examples.
>>>Why don't you try 7/8 (0.875), 15/16 (0.9275), or 31/32 (0.96875) as the
fractional part of the numbers and see if you can get the same behavior.
I'm guessing that you may not.
>>>Any fraction with a reasonable denomiator (< 2^16) that is a power of 2
would be a reasonable test case.
>>>Note that these are all exactly representable by a typical machine.
>>>Just a guess.

This is the exact same problem you reported in comp.databases.mysql, and
the solution is exactly the same.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Dec 27 '06 #5
Has i told you on the other newsgroup

This 2 servers have the same hardware configuration

On Windows 2003
http://efeito.org/info.php

On Linux
http://efeito.net/info.php

On Dec 27, 12:54 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
kkmi...@gmail.com wrote:
PHP code
echo "round(10.045,2) - ".round(10.045,2)."<br />";
echo "round(20.045,2) - ".round(20.045,2)."<br />";
echo "round(30.045,2) - ".round(30.045,2)."<br />";
echo "round(40.045,2) - ".round(40.045,2)."<br />";
This is very strange:
round(10.045,2) - 10.04
round(20.045,2) - 20.05
round(30.045,2) - 30.05
round(40.045,2) - 40.05
On Dec 26, 11:57 am, "kkmi...@gmail.com" <kkmi...@gmail.comwrote:
>I've tested the same function in the same hardware configuration and
the only difference is the OS, so it must be the libraries the big
problem is that i don't know witch ones to replace in order to get the
system to solve the way i want.
>On Dec 22, 4:42 pm, "David T. Ashley" <d...@e3ft.comwrote:
>><kkmi...@gmail.comwrote in messagenews:11**********************@42g2000cwt.go oglegroups.com...
>>>Can some one explain if this can be fixed using php.ini settings ?
>>>echo "round 20.545 -".round(20.545,2)."<br>";
echo "round 20.555 -".round(20.555,2)."<br>";
echo "number_format 20.545 -".number_format(20.545, 2, ',',
'.')."<br>";
echo "number_format 20.555 -".number_format(20.555, 2, ',',
'.')."<br>";
>>>PHP Version 4.3.0 / FreeBSD
round 20.545 -20.55
round 20.555 -20.56
number_format 20.545 -20,55
number_format 20.555 -20,55
>>>PHP Version 4.4.4 / CENTOS
round 20.545 -20.55
round 20.555 -20.55
number_format 20.545 -20,55
number_format 20.555 -20,55
>>>PHP Version 5.1.4 / Windows NT
round 20.545 -20.55
round 20.555 -20.56
number_format 20.545 -20,55
number_format 20.555 -20,56My only observation about the examples you chose is that the fractional
>>parts are infinitely repeating "radiximals" in binary.
>>0.545 = 545/1000 = 109/200 (irreducible)
0.555 = 555/1000 = 111/200 (irreducible)
>>Both of the numbers above can't be expressed as precise binary floating
point numbers because the denominator after reduction has prime factors that
are not "2".
>>PHP.INI may be a factor, but it may also be that something at a lower level
is occurring (i.e. machine arithmetic, how certain rounding settings on
floating-point processors or libraries are set).
>>In other words, you've chosen antagonistic examples.
>>Why don't you try 7/8 (0.875), 15/16 (0.9275), or 31/32 (0.96875) as the
fractional part of the numbers and see if you can get the same behavior.
I'm guessing that you may not.
>>Any fraction with a reasonable denomiator (< 2^16) that is a power of 2
would be a reasonable test case.
>>Note that these are all exactly representable by a typical machine.
>>Just a guess.This is the exact same problem you reported in comp.databases.mysql, and
the solution is exactly the same.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Dec 27 '06 #6
<kk*****@gmail.comwrote in message
news:11*********************@73g2000cwn.googlegrou ps.com...
PHP code
echo "round(10.045,2) - ".round(10.045,2)."<br />";
echo "round(20.045,2) - ".round(20.045,2)."<br />";
echo "round(30.045,2) - ".round(30.045,2)."<br />";
echo "round(40.045,2) - ".round(40.045,2)."<br />";

This is very strange:

round(10.045,2) - 10.04
round(20.045,2) - 20.05
round(30.045,2) - 30.05
round(40.045,2) - 40.05
Why are you posting this code and quoting my post?

..045 = 45/1000 = 9/200 = denominator not a power of 2.

Like your earlier examples, these numbers are not exactly expressable as
base-2 decimals.

It would be interesting to determine the breakpoint between
round(10.045,2) - 10.04
round(20.045,2) - 20.05
where the rounding changes. I'm going to guess that 15.045 is one way and
16.045 is the other.

Dav.e

Dec 27 '06 #7
kk*****@gmail.com wrote:
On Dec 27, 12:54 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
>>kkmi...@gmail.com wrote:
>>>PHP code
echo "round(10.045,2) - ".round(10.045,2)."<br />";
echo "round(20.045,2) - ".round(20.045,2)."<br />";
echo "round(30.045,2) - ".round(30.045,2)."<br />";
echo "round(40.045,2) - ".round(40.045,2)."<br />";
>>>This is very strange:
>>>round(10.045,2) - 10.04
round(20.045,2) - 20.05
round(30.045,2) - 30.05
round(40.045,2) - 40.05
>>>On Dec 26, 11:57 am, "kkmi...@gmail.com" <kkmi...@gmail.comwrote:
>>>>I've tested the same function in the same hardware configuration and
the only difference is the OS, so it must be the libraries the big
problem is that i don't know witch ones to replace in order to get the
system to solve the way i want.
>>>>On Dec 22, 4:42 pm, "David T. Ashley" <d...@e3ft.comwrote:
>>>>><kkmi...@gmail.comwrote in messagenews:11**********************@42g2000cwt.go oglegroups.com...
>>>>>>Can some one explain if this can be fixed using php.ini settings ?
>>>>>>echo "round 20.545 -".round(20.545,2)."<br>";
>>echo "round 20.555 -".round(20.555,2)."<br>";
>>echo "number_format 20.545 -".number_format(20.545, 2, ',',
>>'.')."<br>";
>>echo "number_format 20.555 -".number_format(20.555, 2, ',',
>>'.')."<br>";
>>>>>>PHP Version 4.3.0 / FreeBSD
>>round 20.545 -20.55
>>round 20.555 -20.56
>>number_format 20.545 -20,55
>>number_format 20.555 -20,55
>>>>>>PHP Version 4.4.4 / CENTOS
>>round 20.545 -20.55
>>round 20.555 -20.55
>>number_format 20.545 -20,55
>>number_format 20.555 -20,55
>>>>>>PHP Version 5.1.4 / Windows NT
>>round 20.545 -20.55
>>round 20.555 -20.56
>>number_format 20.545 -20,55
>>number_format 20.555 -20,56My only observation about the examples you chose is that the fractional
>>>>>parts are infinitely repeating "radiximals" in binary.
>>>>>0.545 = 545/1000 = 109/200 (irreducible)
>0.555 = 555/1000 = 111/200 (irreducible)
>>>>>Both of the numbers above can't be expressed as precise binary floating
>point numbers because the denominator after reduction has prime factors that
>are not "2".
>>>>>PHP.INI may be a factor, but it may also be that something at a lower level
>is occurring (i.e. machine arithmetic, how certain rounding settings on
>floating-point processors or libraries are set).
>>>>>In other words, you've chosen antagonistic examples.
>>>>>Why don't you try 7/8 (0.875), 15/16 (0.9275), or 31/32 (0.96875) as the
>fractional part of the numbers and see if you can get the same behavior.
>I'm guessing that you may not.
>>>>>Any fraction with a reasonable denomiator (< 2^16) that is a power of 2
>would be a reasonable test case.
>>>>>Note that these are all exactly representable by a typical machine.
>>>>>Just a guess.This is the exact same problem you reported in comp.databases.mysql, and

the solution is exactly the same.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

Has i told you on the other newsgroup

This 2 servers have the same hardware configuration

On Windows 2003
http://efeito.org/info.php

On Linux
http://efeito.net/info.php
(Top posting fixed)

And as I and others have told you - when using floating point, results
when using expressions like these are unpredictable.

It's just like when using base 10:

round((1.0/3.0) + (1.0/6.0))

can equal zero, because neither 1.0/3.0 nor 1.0/1.6 can be expressed as
an exact value in base 10. They are repeating decimals.

Sure, they *should* come out to 0.5. But their actual value will be
0.499999999 to however many decimal places could be handled.

*Some* libraries will "fix" this by adding a small value (i.e.
0.000000001) to the results to "fudge" the value. But no such fudge
factor is required, nor is it always good.

You are always best to use integers if you need exact values, or add
your own fudge factor to the results.

It's been that way for the almost 40 years I've been programming, and
it's not going to change now. That's why many processors now have a
"packed decimal" or a "binary coded decimal" type internally to give
exact results.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Dec 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matias Silva | last post by:
Can anybody tell me why I am getting rounding errors using the ROUND function. 3.7125 rounds to 3.70 when I use the following: TRUNCATE(ROUND(units_pay_amount * fees_amount, 2),2))) The correct...
6
by: Penguin | last post by:
At some long ago time Steve Jorgensen answered thus: Subject: Re: How can I round a time? Newsgroups: comp.databases.ms-access Date: 1998/12/11 Access represents a date internally as a double...
17
by: nomenklatura | last post by:
Hi, System.Math.Round function is confused me. for example i want to round 3.245 in with decimal symbol Result should be = 3.25 When i try to this in vb: A = 3.245 X = Round(A, 2) then...
9
by: Ronald W. Roberts | last post by:
I'm having a problem understanding the Round function. Below are quotes from two books on VB.NET. The first book shows examples with one argument and how it rounds. The second book something...
4
by: Fuzzydave | last post by:
I have been using a round command in a few places to round a value to zero decimal places using the following format, round('+value+', 0) but this consistantly returns the rounded result of...
10
by: David Coleman | last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1) In the Command Window I get the following ? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2)...
3
by: Krishna.K.1900 | last post by:
Does round() always perfectly return the output expected or are there some artifacts which don't allow perfect functionality Using python 2.5: 12.23 12.234 12.199999999999999 but was...
4
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
9
by: josh logan | last post by:
Hello, I need a round function that _always_ rounds to the higher integer if the argument is equidistant between two integers. In Python 3.0, this is not the advertised behavior of the built-in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.