Jump to content
Larry Ullman's Book Forums

Php & Floating Point Numbers


Recommended Posts

I never realized that PHP was such a disaster at calculating floating point numbers.

 

For instance... 755 x 2 = 1.00 (Ha ha yeah??)

 

http://www.php.net/manual/en/language.types.float.php

 

I noticed this was something not covered in the PHP books Larry or am i once again mistaken? I have noticed from the first book i purchased from you larry Effortless E-commerce that you did your calculations in the database for totals using raw SQL. I was going to make a total column in the database because i thought it would be faster to read out the database than recalculate on loading it up but now i think ill go your way...

 

I can seriously see with this website i am building its basically in some way should i say cocked up but to be honest its just too big for me to get right working alone. I need a team on my site, i will just have to do the best i can now and try to fix it later on with someone more professional than myself. It really seems to be impossible to get it all right first time this is my first website.

 

I don't believe anyone can write a plan for this kind of site and follow it exactly because sometimes it just don't work the way you thought and from what i am seeing code deviates quite greatly from the actual plan. Databases need changed pages need to be recoded, its just impossible, well especially for one person, hahaha.

 

Ah may be its not that bad, code is not like "Counting Cars" on the history channel at least we can completely recode stuff whereas like with Bob Marleys last Mercedes they can't even restore the engine as the parts are just no longer available. B) (What's Up Brother)

Link to comment
Share on other sites

If you are dealing with currency or other things that really requires precision, you need to look up "the money pattern". It's a design pattern invented to deal with precision in floating point number calculations, and do often have conversion capabilities. The basic premise is that you have a unit like dollars, kilometer, miles, gallons, liters or whatever. The pattern is well-known, and you should be able to find it in books, maybe some videos and some free articles.

 

I recommend you start out with this one on NetTuts. It will also explain some basic TDD, which will be really helpful when you deal with this kind of stuff. Because precision is so prone to bugs, and bugs really should never happen when dealing with precision, TDD is almost a must here.

- http://net.tutsplus.com/tutorials/php/money-pattern-the-right-way-to-represent-value-unit-pairs/

 

Keep the moral high btw. Believe in yourself, I know you can do this. :)

Link to comment
Share on other sites

Thanks i like tutsplus i didn't search around as i know i can work out the total using SQL, working on the query now active record based with a cdbcriteria for now. What you mentioned looks stable if i can't find anything else i will go with that, it also has friendly classes excellent.

 

I only have one option to get it done, so i have no choice, so it will be done. Its a shame the Yii Book isn't complete i have to depend on the Yii Definitive guide now and threads online. I am sure once that Yii ecommerce example comes out i will have to go back and probably change a lot of stuff. But this is life i am not blaming you anyway Larry i think you are already taking on more than you can handle, writing that Yii book must be extremely difficult. Even using the Yii you get better and better the more code you write so writing the book alone is incredible.

 

Thanks once again Antonio, i will check that out once i have to deal with money using PHP.

Link to comment
Share on other sites

I got the SQL query to calculate the total but this total is a little bit more complicated, i have shipping_included or free shipping which needs to be added to it. I just tried setting the shipping cost column to NULL to see what would happen and the total came out as $0.00, haha i can see this is going to be fun :) Now its time for some SQL conditional statements if they even exist! Oh yes page 294, 324.

 

Just got the little statement working and it made me happy. I'll post it up!

$criteria = new CDbCriteria();

$criteria->select = 'user_id, price, quantity, IF((shipping_free = \'true\'), (quantity*price), ((quantity*price) + shipping_cost)) AS total , message, shipping_service_type, shipping_included, shipping_cost, shipping_free, shipping_type, date_created';

This is an example of quality but not quantity.

Link to comment
Share on other sites

 Share

×
×
  • Create New...