JavaScript Math vs Excel

L

Lenni

Hi,

I'm currently writing a web application for bicycle wheel builders
that calculate the spoke length for all sorts of variations of hubs
and rims.

I have translated the formula from an Excel spreadsheet and in
JavaScript it looks like this:

var sll=Math.sqrt(Math.pow(((fdl/2*Math.sin((2*Math.PI*cross)))/
(spokes/2)),2)+Math.pow((erd/2-((fdl/2)*Math.cos(2*Math.PI*cross/
(spokes/2)))),2)+Math.pow((c2l+osb),2)-shd/2);

The problem is that I get slightly different results when I use
JavaScript than when I use Open Office Calc. It isn't much, less than
a millimeter, which shouldn't really matter when you're building a
wheel but it is slightly worrying me.

My question, is it possible that the JavaScript Math object is not as
powerful as the Excel or OpenOffice one and that there are just slight
rounding errors that are causing this disparity? Or to rephrase the
question, can I stop checking the formula over and over again for
errors?

Thanks
Lenni
 
D

Dr J R Stockton

In comp.lang.javascript message <834f2e95-9331-46a5-a2c4-43a2792fa288@b1
g2000hsg.googlegroups.com>, Sun, 26 Oct 2008 14:04:23, Lenni
I'm currently writing a web application for bicycle wheel builders
that calculate the spoke length for all sorts of variations of hubs
and rims.

I have translated the formula from an Excel spreadsheet and in
JavaScript it looks like this:

var sll=Math.sqrt(Math.pow(((fdl/2*Math.sin((2*Math.PI*cross)))/
(spokes/2)),2)+Math.pow((erd/2-((fdl/2)*Math.cos(2*Math.PI*cross/
(spokes/2)))),2)+Math.pow((c2l+osb),2)-shd/2);

The problem is that I get slightly different results when I use
JavaScript than when I use Open Office Calc. It isn't much, less than
a millimeter, which shouldn't really matter when you're building a
wheel but it is slightly worrying me.

My question, is it possible that the JavaScript Math object is not as
powerful as the Excel or OpenOffice one and that there are just slight
rounding errors that are causing this disparity? Or to rephrase the
question, can I stop checking the formula over and over again for
errors?

Firstly, you should learn to write legible code.

Secondly, rather than using Math.power(X, 2) I'd suggest using X*X, even
if it means a new temporary variable.

Thirdly, if you had defined your variables and given values, we could
have tested it ourselves.

Fourthly, multiplying by 0.5 can be more legible than dividing by 2.

Lastly, write the expression in "school maths" terms, and evaluate with
Windows Calculator and/or pencil-and-paper. An error of not much less
than a millimetre is of the order of 0.1% of the size of the largest
reasonable bicycle wheel, penny-farthings slightly excepted, and cannot
result from variations in non-broken maths implementations of well-
conditioned expressions.


Furthermore : see how much easier the following, equivalent apart from
possible error, would be, if meaningful names were given to xx6 xx7 xx8
xx9.

twoPiX = 2*Math.PI*cross
xx6 = c2l+osb
xx7 = fdl*Math.sin(twoPiX) / spokes
xx8 = 0.5*(erd - fdl*Math.cos(2*twoPiX/spokes))
xx9 = xx7*xx7 + xx8*xx8 + xx6*xx6 - 0.5*shd
sll = Math.sqrt(xx9);

I assume all input variables are positive. You have two subtractions;
if they are subtractions of only slightly differing numbers, that is
ill-conditioned and sensitive to numerical error - the answer is to
redesign the algorithm so that such subtraction does not occur.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
S

SAM

Le 10/26/08 10:04 PM, Lenni a écrit :
in JavaScript it looks like this:

var sll=Math.sqrt(Math.pow(((fdl/2*Math.sin((2*Math.PI*cross)))/
(spokes/2)),2)+Math.pow((erd/2-((fdl/2)*Math.cos(2*Math.PI*cross/
(spokes/2)))),2)+Math.pow((c2l+osb),2)-shd/2);

The problem is that I get slightly different results when I use
JavaScript than when I use Open Office Calc. It isn't much, less than
a millimeter, which shouldn't really matter when you're building a
wheel but it is slightly worrying me.

Javascript calculate on base 64 (I think, or something like that) and
sometimes it can't give the exactly number such as :
1.0000000009
instead of 1

You could try by using your variables multiplied by 1000 or 100000
and finally get back the roundness of the result divided by the same
coefficient

Perhaps that could work ?
 
J

Joost Diepenmaat

SAM said:
Javascript calculate on base 64 (I think, or something like that)

That is complete crap. The ecmascript specs are quite clear about how JS
numbers are represented: as IEEE 754 floats. It also states that all
numeric operations are according to the IEEE 754 rules. Most languages
that don't have explicit rules follow these in practice (since most CPUs
do).
and sometimes it can't give the exactly number such as : 1.0000000009
instead of 1

And any language doing floating binary calculations will, under certain
(common) circumstances. Binary representation of floating points can not
represent all numbers exactly that decimal representation can.

See http://docs.sun.com/source/806-3568/ncg_goldberg.html
 
E

Evertjan.

SAM wrote on 27 okt 2008 in comp.lang.javascript:
avascript calculate on base 64 (I think, or something like that) and
sometimes it can't give the exactly number such as :
1.0000000009
instead of 1

You could try by using your variables multiplied by 1000 or 100000
and finally get back the roundness of the result divided by the same
coefficient

Such division could sometimes introduce the same kind of error, methinks.

Better use regex do a string manipulation imitating such division:


var aNumber = 1.2345
var resultString = (aNumber*1000+.5).toString().replace(/(\d\d\d)$/,'.$1')
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
var aNumber = 1.2345
var resultString = (aNumber*1000+.5).toString().replace(/(\d\d\d)$/,'.$1')

Fails rather badly on numbers over about 1e18, though a Zimbabwean
economist might not notice; also on small numbers.
 
R

RobG

Care to explain that remark about the Zimbabwean economist?

The economy is a complete basket case, I think it is an oblique
reference to the inflation rate of about 231,000,000% (yes, two
hundred and thirty one million percent)[1] and probably rising -
fast. It was a mere 11,200,00%[2] two months ago.
 
G

gimme_this_gimme_that

Try writing the code in VBScript.

My experience is that you'll *never* get JavaScript and Excel VBA to
return *identical* results when there is lots of math and rounding.

Not tested - but the function might look like this:

<script language="VBScript">
<!--
Function SLL(cross,erd,spokes,erd,osb)
SSL =Math.sqrt(Math.pow(((fdl/2*Math.sin((2*Math.PI*cross)))/ (spokes/
2)),2)+Math.pow((erd/2-((fdl/2)*Math.cos(2*Math.PI*cross/ (spokes/
2)))),2)+Math.pow((c2l+osb),2)-shd/2);
End Function
//-->
</script>

This script is tested and works in both Excel VBA and VBScript.

<script language="VBScript">
<!--
Function VBRound(a, b)
Result = ""
If 0 = b Then
Result = ""
ElseIf "" = b Then
Result = a
ElseIf 0 = a then
Result = 0
Else
Result = b * ((a \ b) - CInt(((a Mod b) >= (b / 2))))
End If
VBRound = Result
End Function
//-->
</script>
 
G

gimme_this_gimme_that

Speaking of VBScript - the strategy you should take is to write the
Function in Excel VBA - then translate to VBScript.

You started with Excel formulas - you need to start with Excel VBA.
 
S

sasuke

SAM wrote on 27 okt 2008 in comp.lang.javascript:



Such division could sometimes introduce the same kind of error, methinks.

Better use regex do a string manipulation imitating such division:

var aNumber = 1.2345
var resultString = (aNumber*1000+.5).toString().replace(/(\d\d\d)$/,'.$1')

var aNumber = 1.2345678;
var resultString = (aNumber*1000+.5).toString().replace(/(\d\d\d)$/,'.
$1');
alert(resultString); // 1235.0.678

../sasuke
 
E

Evertjan.

sasuke wrote on 30 okt 2008 in comp.lang.javascript:
1')

var aNumber = 1.2345678;
var resultString = (aNumber*1000+.5).toString().replace(/(\d\d\d)$/,'.
$1');
alert(resultString); // 1235.0.678

You are right, should be[, only where the absolute value is above 1]:

var aNumber = 1.2345678;
var resultString =
Math.floor(aNumber*1000+.5).toString().replace(/(\d\d\d)$/,'.$1');
alert(resultString); // 1.235
 
E

Evertjan.

Evertjan. wrote on 30 okt 2008 in comp.lang.javascript:
sasuke wrote on 30 okt 2008 in comp.lang.javascript:
$/,'.$
1')

var aNumber = 1.2345678;
var resultString = (aNumber*1000+.5).toString().replace(/(\d\d\d)$/,'.
$1');
alert(resultString); // 1235.0.678

You are right, should be[, only where the absolute value is above 1]:

var aNumber = 1.2345678;
var resultString =
Math.floor(aNumber*1000+.5).toString().replace(/(\d\d\d)$/,'.$1');
alert(resultString); // 1.235

Wow, not that easy in regex, try:

<script type='text/javascript'>

function round3dec(x) {
x = Math.floor(x*1000+.5)+'';
var s = x.replace(/(^\-?).*/,'$1');
x = x.replace(/^\-?/,'0000');
return s + (1*x.replace(/\d\d\d$/,'')) + '.' +
x.replace(/-?\d*(\d\d\d)$/,'$1');
};

alert( round3dec(21.2345678) );
alert( round3dec(-21.2345678) );
alert( round3dec(0.0025555) );
alert( round3dec(-0.0025555) );

</script>
 
S

sasuke

(1000000000000000000).toFixed(3)
  -> 1000000000000000000.000

round3dec(1000000000000000000)
  -> 1e+21.00001e+21

:)

LOL, that's a nice one. Moral of the story: Use built-in functions
whenever possible. ;-)

../sasuke
 
D

Dr J R Stockton

In comp.lang.javascript message <ca9917a8-230f-48df-a016-1efe5081a3fa@i1
8g2000prf.googlegroups.com>, Fri, 31 Oct 2008 07:15:36, sasuke
LOL, that's a nice one. Moral of the story: Use built-in functions
whenever possible. ;-)

But only where they are trustworthy on "all" systems. Method toFixed
has errors in IE.
 
E

Evertjan.

sasuke wrote on 31 okt 2008 in comp.lang.javascript:
LOL, that's a nice one. Moral of the story: Use built-in functions
whenever possible. ;-)

Not at all, there is no joy in using build-in functions,
but for those who love production.

The above example of conrad is out of bounds in normal use.

Regex is a joyful puzzle by itself.
 

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top