Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Codes fails if sum total is around .8 - why ?

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 03-09-2013, 10:16 AM PM User | #1 Reflo2 New Coder   Join Date: Mar 2013 Posts: 11 Thanks: 2 Thanked 0 Times in 0 Posts Codes fails if sum total is around .8 - why ? Hi all again Can anyone tell me what I'm doing wrong as this code "seems" to work until the total reaches around .8 You can test this by adding 12.34 to the 1st, 2nd and 3rd boxes then you get an error - but if you change the last 12.34 to 12.35 it works fine. The same happens if you add 50.7 to the 1st and then 50.1 to the 2nd - but it will work if the answer is not around .8 Thanks if you can spot my basic mistake - even better if you can suggest a better (simpler) method, as this is doing my head in :-) Karen Code: ```
Total:
```
 03-09-2013, 04:30 PM PM User | #2 Reflo2 New Coder   Join Date: Mar 2013 Posts: 11 Thanks: 2 Thanked 0 Times in 0 Posts and it seems it's not just me. http://www.texaswebdevelopers.com/ex...javascript.asp This is the same problem (with a different code) from someone teaching people how to write code. On thier website try adding 50.1 to the 1st box and 50.1 to the second - same problem. I never was much use a math at school so I can't work this out What am I missing please ??
 03-09-2013, 04:33 PM PM User | #3 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,490 Thanks: 200 Thanked 2,471 Times in 2,449 Posts I take it you mean that 12.34 + 12.34 + 12.34 = 37.019999999999996 and 50.7 + 50.1 = 100.80000000000001 It happens because computers work in binary so they convert your number to the nearest binary equivalent first and then convert the binary back into the nearest decimal equivalent at the end. ECMAScript numbers are represented in binary as IEEE-754 (IEC 559) Doubles, with a resolution of 53 bits, giving an accuracy of 15-16 decimal digits; integers up to just over 9e15 are precise, but few decimal fractions are. Given this, arithmetic is as exact as possible, but no more. Operations on integers are exact if the true result and all intermediates are integers within that range. Other examples:- Code: `````` If you need an exact answer to a set number of decimal places then shift the decimal point in all your numbers that many places to the right to make integers before doing the calculation and then shift it back after the calculation. For example with currencies that use two decimal places you should always multiply them all by 100 at the start and divide by 100 at the end or the answer might not be exact. Code: `````` “What is a cynic? A man who knows the price of everything and the value of nothing.” - Oscar Wilde (Irish Poet, Novelist, Dramatist and Critic, 1854-1900) __________________ All the code given in this post has been tested and is intended to address the question asked. Unless stated otherwise it is not just a demonstration. Last edited by Philip M; 03-09-2013 at 04:42 PM..
 Users who have thanked Philip M for this post: Reflo2 (03-09-2013)
 03-09-2013, 05:07 PM PM User | #4 Airblader Regular Coder   Join Date: Jan 2013 Location: Germany Posts: 578 Thanks: 4 Thanked 77 Times in 77 Posts One addition: Be careful what kind of operations you're doing, though. With addition and subtraction it works fine, but multiplication, for example, will get you in trouble: Code: ```var a = 4, b = 5; var aShifted = 100 * a, bShifted = 100 * b; // Addition works console.log( a + b ); // 9 console.log( ( aShifted + bShifted ) / 100 ); // 9 // Multiplication not so much console.log( a * b ); // 20 console.log( ( aShifted * bShifted ) / 100 ); // 2000``` Of course this doesn't come surprising if you know elementary school math, but there is a certain risk that one doesn't think about it. Not that the trick wouldn't work in this case, it just has to be slightly modified: Code: `console.log( ( aShifted * bShifted ) / 10000 ); // 20` In other words: It's not as easy as simply dividing by what you multiplied the numbers with in the beginning. You'll actually have to think about what to divide by in the end.
 03-09-2013, 05:15 PM PM User | #5 Reflo2 New Coder   Join Date: Mar 2013 Posts: 11 Thanks: 2 Thanked 0 Times in 0 Posts Hi Philip i have tried to *100 then /100 within the code but it either does nothing only gives the answer 0. eg Code: ``` var sum = add(((value1*100), (value2*100), (value3*100), (value4*100), (value5*100), (value6*100), (value7*100), (value8*100))/100); \$('input:text[id\$=TSum]').val(sum);``` Where would I add the calculation please Karen
 03-09-2013, 05:17 PM PM User | #6 Reflo2 New Coder   Join Date: Mar 2013 Posts: 11 Thanks: 2 Thanked 0 Times in 0 Posts Hi Philip i have tried to *100 then /100 within the code but it either does nothing only gives the answer 0. eg Code: ``` var sum = add(((value1*100), (value2*100), (value3*100), (value4*100), (value5*100), (value6*100), (value7*100), (value8*100))/100); \$('input:text[id\$=TSum]').val(sum);``` Where would I add the calculation please Karen ps - you are right in that users are adding currency (UK Pound) amounts
 03-09-2013, 05:22 PM PM User | #7 Airblader Regular Coder   Join Date: Jan 2013 Location: Germany Posts: 578 Thanks: 4 Thanked 77 Times in 77 Posts It's probably better to adjust your add method: Code: ```function add() { var sum = 0; for (var i = 0, j = arguments.length; i < j; i++) { if (IsNumeric(arguments[i])) { sum += 100 * parseFloat(arguments[i]); } } return sum / 100; }```
 Users who have thanked Airblader for this post: Reflo2 (03-09-2013)
 03-09-2013, 05:42 PM PM User | #8 Reflo2 New Coder   Join Date: Mar 2013 Posts: 11 Thanks: 2 Thanked 0 Times in 0 Posts Thank you Philip_M and Airbladder Your help has solved a problem I have been working on for 2 days.
03-09-2013, 06:06 PM   PM User | #9
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,490
Thanks: 200
Thanked 2,471 Times in 2,449 Posts
Quote:
 Originally Posted by Reflo2 Thank you Philip_M and Airbladder
ROTFL How appropriate!

Reminds me of a message stuck on the front of one of those hot-air hand-drying machines in a university toilet

"To listen to a lecture by Professor X press here".

We are sorry that as a result of a typographical error we referred to the now deceased but well-known English actor Denis Prick.
This should of course have read Pe*is Price. - Daily Mirror
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

03-09-2013, 08:42 PM   PM User | #10
Regular Coder

Join Date: Jan 2013
Location: Germany
Posts: 578
Thanks: 4
Thanked 77 Times in 77 Posts
Quote:
 Originally Posted by Philip M How appropriate!
I did not criticize or attack you in this thread at all. If you want to make everything personal from now on because we have different views on some topics, be my guest. Let's start with the good laugh I get every time you complain about books and tutorials using "out-dated technology that became obsolete a long time ago", even though your code "style" became obsolete a decade ago and yet you go on teaching it to beginners. How's that for a hot air lecture? And how appropriate that your quote is about typography, which is partly about making what you do look nice and readable.

You'd think someone of your age is a little less of a child.

Last edited by Airblader; 03-09-2013 at 08:55 PM..

03-09-2013, 09:43 PM   PM User | #11
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,490
Thanks: 200
Thanked 2,471 Times in 2,449 Posts
Quote:
 Originally Posted by Airblader I did not criticize or attack you in this thread at all.

Would you be surprised to learn that nobody but you has ever raised that objection, in this forum or anywhere else? So what is it that you feel gives you (a newcomer to the forum) a licence to be so critical and combative?

I quite understand that you have no sense of humour, and feel unrestrained by any lack of self-esteem. But be aware that my motto is the same as that of the Kings of Scotland - Nemo me impune lacessit.

I see the purpose of this forum is to offer assistance to the OPs. Not to manufacture quarrels with other contributors.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

03-09-2013, 09:55 PM   PM User | #12
Regular Coder

Join Date: Jan 2013
Location: Germany
Posts: 578
Thanks: 4
Thanked 77 Times in 77 Posts
Quote:
 Originally Posted by Philip M So what is it that you feel gives you (a newcomer to the forum) a licence to be so critical and combative?
All I did was criticize things that I find worth discussing. If you don't feel the same way, you are always welcome to not participate in such discussions. I don't see any rule or reason why I need to wait a few years before I'm allowed to have my opinion and share my thoughts on other people's posts. Just because you're older, just because you have been in this forum longer, your opinion is not "worth" more than mine (and vice versa).

Quote:
 I see the purpose of this forum is to offer assistance to the OPs. Not to manufacture quarrels with other contributors.
I saw that when I posted in this thread, adding a little warning to your post and helping the OP to rewrite his code, when you replied with a whole post dedicated to offending me.

03-10-2013, 09:44 AM   PM User | #13
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,490
Thanks: 200
Thanked 2,471 Times in 2,449 Posts
Quote:
 Originally Posted by Airblader All I did was criticize things that I find worth discussing. If you don't feel the same way, you are always welcome to not participate in such discussions. I don't see any rule or reason why I need to wait a few years before I'm allowed to have my opinion and share my thoughts on other people's posts. Just because you're older, just because you have been in this forum longer, your opinion is not "worth" more than mine (and vice versa).

Get wise to yourself. Most people would accept that in any subject the opinion of a Johnny-Come-Lately is not worth that of (far) older and more experienced people. You will recall that you received absolutely zero support from others in this forum.

What you are saying is that you are free to make posts which are dedicated to offending me, but start to scream when the position is reversed.

It was not me who corrupted your screen name to the comic Airbladder. All I did was laugh at something I found funny.
You would call that sharing my thoughts. And you frequently mis-spell my name.

But I have just realised that Airblade is the name of James Dyson's high-tech hand dryer which pumps out a blast of hot air. But you are quite free not to participate in any discussion about that!
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

 03-10-2013, 09:55 AM PM User | #14 Reflo2 New Coder   Join Date: Mar 2013 Posts: 11 Thanks: 2 Thanked 0 Times in 0 Posts I am truly sorry if I have offended anyone by misspelling their name, that was not my intention. I, like many people, having been browsing this forum for quite some time as it is an extremely valuable resource. It was only when I couldn’t find the answer I was looking for that I decided to join, so I could post a specific question. I am not sure if there is a way that I can “redo” my thanks so that I can alter the spelling. If there is a way can someone please tell me. Once again I am very sorry and I honestly didn’t mean to offend anyone. Karen PS. He is a She :-)
03-10-2013, 10:34 AM   PM User | #15
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,490
Thanks: 200
Thanked 2,471 Times in 2,449 Posts
Quote:
 Originally Posted by Reflo2 I am truly sorry if I have offended anyone by misspelling their name, that was not my intention. I, like many people, having been browsing this forum for quite some time as it is an extremely valuable resource. It was only when I couldn’t find the answer I was looking for that I decided to join, so I could post a specific question. I am not sure if there is a way that I can “redo” my thanks so that I can alter the spelling. If there is a way can someone please tell me. Once again I am very sorry and I honestly didn’t mean to offend anyone. Karen PS. He is a She :-)

Karen - There is absolutely no need to apologise. Most people would just laugh at your innocent typo. Only the over-sensitive and over-serious would take offence. Airblader (who is devoid of a sense of humour) is cross with me (not you) for making fun of him.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 03:54 PM.