Rounding error in Phalanger compared to PHP
5 posts
• Page 1 of 1
Rounding error in Phalanger compared to PHP
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
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
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. | @misekjakub │ jakub@devsense.com │
-
Jakub Misek - Posts: 2092
- Joined: January 4th, 2012, 2:42 pm
- Location: Prague
Re: Rounding error in Phalanger compared to PHP
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
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
ok
Jakub Misek │ DEVSENSE s.r.o. | @misekjakub │ jakub@devsense.com │
-
Jakub Misek - Posts: 2092
- Joined: January 4th, 2012, 2:42 pm
- Location: Prague
5 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 16 guests