On Sun, Jul 19, 2009 at 03:38, Octavian Râsnita<orasn...@gmail.com> wrote: > From: "Jim Gibson" <jimsgib...@gmail.com> >> >> PHP is just hiding the inaccuracies of representing decimal fractions >> using binary numbers, because the inaccuracy is insignificant in most >> applications. > > Well, if it wouldn't be significant, why would PHP hide that inaccuracy as a > default? Why doesn't Perl do the same thing?
PHP is designed to make web sites quickly. Perl is a general purpose programming language. PHP is designed for people who know little-to-no Computer Science and would be confused by how floating point math works. Perl is designed for all sorts of people, and therefore tries to smooth over those areas that do not matter much (like memory management), but leaves the rough edges where there is no good solution (e.g. floating point). > >> Using Bignum means doing arithmetic with more than 64 bits. 64-bit >> floating-point numbers are 1) accurate enough for almost all applications >> and 2) supported by hardware, which means they are very fast. Using more >> bits for more accuracy means the arithmetic cannot be done with hardware >> instructions alone, but must be done with multiple instructions in software. >> As a result, the calculations will take an order of magnitude longer. > > Well, in that case Perl could use the same way PHP uses. > The calculations made with PHP are faster even if PHP also need to also hide > that inaccuracy. PHP is not using bignum, it is just lying (i.e. rounding). > >> It is stupid to penalize every program to accomplish what only a very few >> programs need. > > It is true, and maybe I am wrong because I need to do many calculations > using money, but if "most of the programs" wouldn't need this inaccuracy > hiding, why PHP, Oracle, PostgreSQL and MySQL use this way of hinding the > inaccuracy automaticly? You should be working with an integer number of pennies if you are dealing with money. Another solution is to use the decimal/number type. This type uses [BCD][0] and is slower than floating point math, but, within its level of precision, it is more accurate (if I have used those technical terms correctly). In Oracle, if you say something like NUMBER(14, 2) then you are using BCD. In PostgreSQL and MySQL it is called DECIMAL(14, 2). > > I don't want to say that if Windows is used by most users this means that it > is the best. They all could be wrong and perl good, but the reality is that > a perl programmer would need to do more manual coding than a PHP programmer > and this is what I don't like. It is not that I don't know how to do it, but > the solutions are either not nice or very slow, while the solution found by > PHP is very fast and it doesn't imply other things than just making the > calculation. And very wrong. It is hiding something you need to know from you. Prior to this you thought you were dealing with sensible math, but now you know better. You were doing floating point math involving money in PHP thinking everything was fine when this is one of the biggest errors in the financial world. Perl has clued you in on the fact that floating point numbers are approximations, even if you haven't fully grasped what that means yet. > >> The solution has been given to you: print out floating-point numbers with >> the appropriate number of digits using printf and the format descriptor >> "%.mf", where m is the number of digits you want. If you actually need more >> than 64 bits (and you probably don't), then use Math::BigFloat and suffer >> the performance penalty. > > True. I don't need more than 4 or 8 digits, but I don't know how to do this > transparently without needing to use sprintf() for each calculation. You only round at the end, and then only when you display the value; you do not store rounded values. [0] : http://en.wikipedia.org/wiki/Binary-coded_decimal -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/