Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 14 of 14

07132004, 12:11 PM #1
 Join Date
 Jul 2004
 Posts
 19
 Thanks
 0
 Thanked 0 Times in 0 Posts
Calculation error, different result from excel
I'm having difficulty with some calculation I'm trying to achieve in javascript
I'm converting this Excel formula
=E15*12/1.25/5.15*100 (where E15 is a random, say 550) and the answer is 102524.2718.
When I converted this into a java script calculation I did this.
var maxMortgage65 = Math.ceil(CurRent * 12 / 1.25 / payRate65 * 100);
(payRate in this example would be 5.15)
But this gave an answer of 10252500, which is wrong.
So I've tried to rail the calculation to force it to do it in order
var temp = Math.ceil(CurRent * 12);
var temp2 = Math.ceil(temp / 1.25);
var temp3 = Math.ceil(temp2 / payRate65);
var temp4 = Math.ceil(temp3 * 100);
var maxMortgage65 = Math.ceil(temp4);
Which gave the same answer, 10252500. Why is javascript doing this? I've tried it without the Math.ceil method but that made no difference (except adding numbers after the decimal point). Any ideas?
07132004, 12:20 PM
#2
 Join Date
 Apr 2003
 Location
 Bucharest, ROMANIA
 Posts
 8,478
 Thanks
 58
 Thanked 379 Times in 375 Posts
for me
Math.ceil(550*12/1.25/5.5*100)
gives
96000
07132004, 12:23 PM
#3
 Join Date
 Jul 2004
 Posts
 19
 Thanks
 0
 Thanked 0 Times in 0 Posts
was that in excel or in a calculator? If in a calculator then you need to press equals after every action. Any ideas?
07132004, 12:32 PM
#4
 Join Date
 Jun 2002
 Location
 Philippines
 Posts
 11,043
 Thanks
 0
 Thanked 251 Times in 247 Posts
Try putting parentheses to specify the intended precedence.
Math.ceil( ( (CurRent * 12) / 1.25 ) / (payRate65 * 100) );
Is that what you intended to do?
Glenn
____________________________________
My Blog
Tower of Hanoi Android app (AdFREE!)
Tower of Hanoi Android app (FREE!)
Tower of Hanoi Leaderboard
Samegame Facebook App
Samegame Facebook App Leaderboard
vBulletin 5 Floating Bar Mod
vBulletin 5 Photo Slideshow Mod
vBulletin 5 Quote Selected Text Mod
vBulletin 5 Profile Preview Tooltip Mod
____________________________________
07132004, 12:51 PM
#5
 Join Date
 Apr 2003
 Location
 Bucharest, ROMANIA
 Posts
 8,478
 Thanks
 58
 Thanked 379 Times in 375 Posts
It's the same thing, as the * and / are fully associative operators one to eachotherTry putting parentheses to specify the intended precedence.
07132004, 12:55 PM
#6
 Join Date
 Jul 2004
 Posts
 19
 Thanks
 0
 Thanked 0 Times in 0 Posts
It's what I'm trying to achieve, but never seems to work, I either get the result
10252428 or 1026 (from glenngv)
I can never get the same answer as excel, which is 102524.2718 (Not fussed about the numbers after the decimal.
I've been trying for hours to get this to work correctly. Even tried dividing the answer by 100 to get rid of the last two digits but that doesn't work for all figures.
07132004, 12:59 PM
#7
 Join Date
 Jun 2002
 Location
 Philippines
 Posts
 11,043
 Thanks
 0
 Thanked 251 Times in 247 Posts
These give 2 different answers when I run it in the address bar. My browser is Firefox.Originally Posted by Kor
javascript:alert(Math.ceil(550 * 12 / 1.25 / 5.15 * 100));alert(Math.ceil(((550 * 12) / 1.25) / (5.15 * 100)))
The first alert gives 102525, 2nd gives 11.
Last edited by glenngv; 07132004 at 01:03 PM.
Glenn
____________________________________
My Blog
Tower of Hanoi Android app (AdFREE!)
Tower of Hanoi Android app (FREE!)
Tower of Hanoi Leaderboard
Samegame Facebook App
Samegame Facebook App Leaderboard
vBulletin 5 Floating Bar Mod
vBulletin 5 Photo Slideshow Mod
vBulletin 5 Quote Selected Text Mod
vBulletin 5 Profile Preview Tooltip Mod
____________________________________
07132004, 01:05 PM
#8
 Join Date
 Apr 2003
 Location
 Bucharest, ROMANIA
 Posts
 8,478
 Thanks
 58
 Thanked 379 Times in 375 Posts
I reckon I don't quite well understand what that EXCEL formula does. As I repeat, when I operate (javascript or even a simple calculator) the operation:
550 'multiplied with' 12 'divided by' 1.25 'divided by' 5.5 'multiplied with' 100
the result is obviously 96000
So can you translate the Excel calculation in a simple linear math calculation?
07132004, 01:06 PM
#9
 Join Date
 Jun 2002
 Location
 Philippines
 Posts
 11,043
 Thanks
 0
 Thanked 251 Times in 247 Posts
As you see on my previous post, your code works for me. It gave me 102525. There's no decimal point as it was rounded up because of the Math.ceil method. Removing it will give you 102524.2718446602, which is close to what Excel produces.Originally Posted by Furton
Glenn
____________________________________
My Blog
Tower of Hanoi Android app (AdFREE!)
Tower of Hanoi Android app (FREE!)
Tower of Hanoi Leaderboard
Samegame Facebook App
Samegame Facebook App Leaderboard
vBulletin 5 Floating Bar Mod
vBulletin 5 Photo Slideshow Mod
vBulletin 5 Quote Selected Text Mod
vBulletin 5 Profile Preview Tooltip Mod
____________________________________
07132004, 01:16 PM
#10
 Join Date
 Jul 2004
 Posts
 19
 Thanks
 0
 Thanked 0 Times in 0 Posts
Got it working, thanks all
Think I might be using this forum more often now
07132004, 01:17 PM
#11
 Join Date
 Jun 2002
 Location
 Philippines
 Posts
 11,043
 Thanks
 0
 Thanked 251 Times in 247 Posts
My test results show that division takes precedence over multiplication.
In essence, this:
alert(Math.ceil(550 * 12 / 1.25 / 5.15 * 100));
is the same as:
alert(Math.ceil(550 * (12 / 1.25 / 5.15) * 100));
Both gives 102525.
So my original suggestion of putting parentheses to specify the intended operator precedence will make the operations clearer and get the expected result.
Glenn
____________________________________
My Blog
Tower of Hanoi Android app (AdFREE!)
Tower of Hanoi Android app (FREE!)
Tower of Hanoi Leaderboard
Samegame Facebook App
Samegame Facebook App Leaderboard
vBulletin 5 Floating Bar Mod
vBulletin 5 Photo Slideshow Mod
vBulletin 5 Quote Selected Text Mod
vBulletin 5 Profile Preview Tooltip Mod
____________________________________
07132004, 01:18 PM
#12
 Join Date
 Jun 2002
 Location
 Philippines
 Posts
 11,043
 Thanks
 0
 Thanked 251 Times in 247 Posts
For reference, could you post your solution?Originally Posted by Furton
Glenn
____________________________________
My Blog
Tower of Hanoi Android app (AdFREE!)
Tower of Hanoi Android app (FREE!)
Tower of Hanoi Leaderboard
Samegame Facebook App
Samegame Facebook App Leaderboard
vBulletin 5 Floating Bar Mod
vBulletin 5 Photo Slideshow Mod
vBulletin 5 Quote Selected Text Mod
vBulletin 5 Profile Preview Tooltip Mod
____________________________________
07132004, 01:18 PM
#13
 Join Date
 Apr 2003
 Location
 Bucharest, ROMANIA
 Posts
 8,478
 Thanks
 58
 Thanked 379 Times in 375 Posts
I have now the same results with glenn (I copied wrong a value). Tested with IE6, Firefox, Opera7.5, NS6, NS7
1. to Furton : how's possible to get 10252428 or 1026 ?
2. to glenn: that is weird... Are there classical math associative and distributive properties not respected by javascript operators?
07132004, 01:42 PM
#14
 Join Date
 Jul 2004
 Posts
 19
 Thanks
 0
 Thanked 0 Times in 0 Posts
Along with the calculation being incorrect I was using values that work for some percentage calculations and not for others.
The payRate var was calculated from two other var's that were 0.0045 and 0.0065, this worked great for doing a simple figure*payRate to discover the percentage but it didn't work correctly in the above calculation. So I changed the var's that calculated pay rate to simply 4.5 and 0.65 which fixed the problem and declared new var's for anything figure*payRate.
This was really a problem created by some sloppy programming by me (I think), but glad it's all done now. Do you want the whole code posted here or in the "post a java script" forum?
Once, appricate the help, the things you said jogged my brain into thinking the problem was further up the code