HighDots Forums  

JavaScript Math vs Excel

Javascript JavaScript language (comp.lang.javascript)


Discuss JavaScript Math vs Excel in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Lenni
 
Posts: n/a

Default JavaScript Math vs Excel - 10-26-2008 , 05:04 PM






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


Reply With Quote
  #2  
Old   
thollister911
 
Posts: n/a

Default Re: JavaScript Math vs Excel - 10-27-2008 , 05:35 AM






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 one-to-one 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 <Lenniboy (AT) googlemail (DOT) com> wrote in news:834f2e95-9331-46a5-a2c4-
43a2792fa288 (AT) b1g2000hsg (DOT) googlegroups.com:

Quote:
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




Reply With Quote
  #3  
Old   
Dr J R Stockton
 
Posts: n/a

Default Re: JavaScript Math vs Excel - 10-27-2008 , 09:12 AM



In comp.lang.javascript message <834f2e95-9331-46a5-a2c4-43a2792fa288@b1
g2000hsg.googlegroups.com>, Sun, 26 Oct 2008 14:04:23, Lenni
<Lenniboy (AT) googlemail (DOT) com> posted:

Quote:
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.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


Reply With Quote
  #4  
Old   
SAM
 
Posts: n/a

Default Re: JavaScript Math vs Excel - 10-27-2008 , 04:47 PM



Le 10/26/08 10:04 PM, Lenni a écrit :
Quote:
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


Reply With Quote
  #5  
Old   
Joost Diepenmaat
 
Posts: n/a

Default Re: JavaScript Math vs Excel - 10-27-2008 , 05:08 PM



SAM <stephanemoriaux.NoAdmin (AT) wanadoo (DOT) fr.invalid> writes:

Quote:
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).

Quote:
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

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/


Reply With Quote
  #6  
Old   
Evertjan.
 
Posts: n/a

Default Re: JavaScript Math vs Excel - 10-28-2008 , 05:03 AM



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

Quote:
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)


Reply With Quote
  #7  
Old   
Dr J R Stockton
 
Posts: n/a

Default Re: JavaScript Math vs Excel - 10-28-2008 , 03:21 PM



In comp.lang.javascript message <Xns9B45664504D47eejj99 (AT) 194 (DOT) 109.133.242>
, Tue, 28 Oct 2008 09:03:18, Evertjan. <exjxw.hannivoort (AT) interxnl (DOT) net>
posted:
Quote:
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 <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)


Reply With Quote
  #8  
Old   
Conrad Lender
 
Posts: n/a

Default Re: JavaScript Math vs Excel - 10-28-2008 , 11:27 PM



On 2008-10-28 20:21, Dr J R Stockton wrote:
Quote:
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


Reply With Quote
  #9  
Old   
RobG
 
Posts: n/a

Default Re: JavaScript Math vs Excel - 10-28-2008 , 11:44 PM



On Oct 29, 1:27*pm, Conrad Lender <crlen... (AT) yahoo (DOT) com> wrote:
Quote:
On 2008-10-28 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/08/19/zimbabwe.inflation/index.html
Quote:
--
Rob


Reply With Quote
  #10  
Old   
Conrad Lender
 
Posts: n/a

Default Re: JavaScript Math vs Excel - 10-29-2008 , 12:07 AM



On 2008-10-29 04:44, RobG wrote:
Quote:
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


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.