P: n/a

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  
Share this Question
P: n/a

Lenni 
Very interesting project. I love math and am an engineer ... are you sure
your algorythm is accurate? A detailed description of the variables you
use within that formula and how you derived each step of the calculation
would be something you might want to get a second opinion on? I am not a
hub expert ... but the amount a spoke rotates axially, the hub width, and
the distance to axle centerline are certainly variables of interest. Rim
diameter and offset need to be considered too.
When you raise a value to a power ... there is usually a great amount of
sensitivety with precision of the exponent. And you do this often in your
formula. I suggest printing out 15+ digits within your spreadsheet to
determine its precision and doing the same within other programs.
Restrict the most precise to the accuracy of the least precise until you
get an exact match. Off by a millimeter is not acceptable in high
performance situations.
Break up your formula into many groupings and compare each onetoone on
each platform ... instead of just looking at the end result. A true error
analysis is not a trivial task.
I am just now learning JavaScript, but I have done a lot of C and C++
programming where precision was very important and any discreptancy had
to be explored fully.
Dig in deeper is my suggestion. I wish I knew JavaScript well enough to
be more helpful. Good luck!
 tom
Lenni <Le******@googlemail.comwrote in news:834f2e95933146a5a2c4 43**********@b1g2000hsg.googlegroups.com:
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
 
P: n/a

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 ?

sm  
P: n/a

SAM <st*********************@wanadoo.fr.invalidwrite s:
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/8063568/ncg_goldberg.html

Joost Diepenmaat  blog: http://joost.zeekat.nl/  work: http://zeekat.nl/  
P: n/a

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')

Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)  
P: n/a

In comp.lang.javascript message <Xn********************@194.109.133.242>
, Tue, 28 Oct 2008 09:03:18, Evertjan. <ex**************@interxnl.net>
posted:
> 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.

(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/ FAQish topics, acronyms, & links.
Proper <= 4line sig. separator as above, a line exactly " " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)  
P: n/a

On 20081028 20:21, Dr J R Stockton wrote:
>>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.
Care to explain that remark about the Zimbabwean economist?
 Conrad  
P: n/a

On Oct 29, 1:27*pm, Conrad Lender <crlen...@yahoo.comwrote:
On 20081028 20:21, Dr J R Stockton wrote:
>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.
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.
1. <URL: http://www.guardian.co.uk/world/2008/oct/09/zimbabwe >
2. <URL: http://edition.cnn.com/2008/BUSINESS...ion/index.html
>

Rob  
P: n/a

On 20081029 04:44, RobG wrote:
>>Fails rather badly on numbers over about 1e18, though a Zimbabwean economist might not notice; also on small numbers.
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%
Oh dear. At that rate they'll soon have their inflation *rate* overflow,
unless somebody anticipated the need for more than 4 bytes to hold that
number. On the other hand, if it's signed, it will just wrap around and
become negative. That should solve the problem.
Thanks for the explanation.
 Conrad  
P: n/a

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>  
P: n/a

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.  
P: n/a

On Oct 28, 2:03*pm, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
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')
var aNumber = 1.2345678;
var resultString = (aNumber*1000+.5).toString().replace(/(\d\d\d)$/,'.
$1');
alert(resultString); // 1235.0.678
../sasuke  
P: n/a

sasuke wrote on 30 okt 2008 in comp.lang.javascript :
>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
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

Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)  
P: n/a

Evertjan. wrote on 30 okt 2008 in comp.lang.javascript :
sasuke wrote on 30 okt 2008 in comp.lang.javascript:
>>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
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>

Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)  
P: n/a

On 20081030 21:59, Evertjan. wrote:
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>
(1000000000000000000).toFixed(3)
1000000000000000000.000
round3dec(1000000000000000000)
1e+21.00001e+21
:)
 Conrad  
P: n/a

On Oct 31, 2:57*am, Conrad Lender <crlen...@yahoo.comwrote:
On 20081030 21:59, Evertjan. wrote:
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>
(1000000000000000000).toFixed(3)
* 1000000000000000000.000
round3dec(1000000000000000000)
* 1e+21.00001e+21
:)
LOL, that's a nice one. Moral of the story: Use builtin functions
whenever possible. ;)
../sasuke  
P: n/a

sasuke wrote on 31 okt 2008 in comp.lang.javascript :
On Oct 31, 2:57*am, Conrad Lender <crlen...@yahoo.comwrote:
>On 20081030 21:59, Evertjan. wrote:
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>
(1000000000000000000).toFixed(3) * 1000000000000000000.000
round3dec(1000000000000000000) * 1e+21.00001e+21
:)
LOL, that's a nice one. Moral of the story: Use builtin functions
whenever possible. ;)
Not at all, there is no joy in using buildin 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.

Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)  
P: n/a

In comp.lang.javascript message <ca9917a8230f48dfa0161efe5081a3fa@i1
8g2000prf.googlegroups.com>, Fri, 31 Oct 2008 07:15:36, sasuke
<da*********@gmail.composted:
> LOL, that's a nice one. Moral of the story: Use builtin functions whenever possible. ;)
But only where they are trustworthy on "all" systems. Method toFixed
has errors in IE.

(c) John Stockton, near London. *@merlyn.demon.co.uk/?.**********@physics.org
Web <URL:http://www.merlyn.demon.co.uk/ FAQish topics, acronyms, & links.
Correct <= 4line sig. separator as above, a line precisely " " (SoRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SoRFC1036)   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 3561
 replies: 17
 date asked: Oct 26 '08
