Rounding error in Phalanger compared to PHP

Discussion about the open-source Phalanger [?] project.

Rounding error in Phalanger compared to PHP

Postby KendallB » February 18th, 2012, 9:47 pm

Phalanger seems to round incorrectly when using the round() function compared to real PHP. Try the following code:

Code: Select all
<?php
echo '<pre>';
echo 'Rounding 4.4 = ' . round(4.4) . "\n";
echo 'Rounding 4.5 = ' . round(4.5) . "\n";
echo 'Rounding 4.500000000000001 = ' . round(4.500000000000001) . "\n";
- Kendall Bennett
KendallB
 
Posts: 340
Joined: January 7th, 2012, 8:36 pm

Re: Rounding error in Phalanger compared to PHP

Postby Jakub Misek » February 18th, 2012, 10:08 pm

As you found at http://stackoverflow.com/questions/9777 ... kidding-me there is Math.Round overload that behaves correctly. But it decreases performance of standard Math.Round(double) significantly.

Another replacement would be to call floor( $number + 0.5 ) ? which behaves incorrectly only for negative numbers with the fraction .5
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague

Re: Rounding error in Phalanger compared to PHP

Postby KendallB » February 18th, 2012, 10:32 pm

I guess the question becomes whether we want Phalanger to implement rounding identically to how native PHP does it, or to implement it how .NET does it. If you want legacy code to work identically, then it should do it the same as real PHP. But if you want it to run the way other .NET code runs, then it should stay the same.
- Kendall Bennett
KendallB
 
Posts: 340
Joined: January 7th, 2012, 8:36 pm

Re: Rounding error in Phalanger compared to PHP

Postby KendallB » February 18th, 2012, 11:54 pm

Maybe we should move this to a public discussion?
- Kendall Bennett
KendallB
 
Posts: 340
Joined: January 7th, 2012, 8:36 pm

Re: Rounding error in Phalanger compared to PHP

Postby Jakub Misek » February 19th, 2012, 12:05 am

ok
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague


Return to Phalanger project

Who is online

Users browsing this forum: No registered users and 16 guests

User Control Panel

Login

Who is online

In total there are 16 users online :: 0 registered, 0 hidden and 16 guests (based on users active over the past 5 minutes)
Most users ever online was 511 on April 27th, 2024, 10:11 pm

Users browsing this forum: No registered users and 16 guests