473,769 Members | 5,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JavaScript Math vs Excel

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(M ath.pow(((fdl/2*Math.sin((2*M ath.PI*cross)))/
(spokes/2)),2)+Math.pow ((erd/2-((fdl/2)*Math.cos(2*M ath.PI*cross/
(spokes/2)))),2)+Math.p ow((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

Oct 26 '08 #1
17 3923
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 <Le******@googl email.comwrote in news:834f2e95-9331-46a5-a2c4-
43**********@b1 g2000hsg.google groups.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(M ath.pow(((fdl/2*Math.sin((2*M ath.PI*cross)))/
(spokes/2)),2)+Math.pow ((erd/2-((fdl/2)*Math.cos(2*M ath.PI*cross/
(spokes/2)))),2)+Math.p ow((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

Oct 27 '08 #2
SAM
Le 10/26/08 10:04 PM, Lenni a écrit :
>
in JavaScript it looks like this:

var sll=Math.sqrt(M ath.pow(((fdl/2*Math.sin((2*M ath.PI*cross)))/
(spokes/2)),2)+Math.pow ((erd/2-((fdl/2)*Math.cos(2*M ath.PI*cross/
(spokes/2)))),2)+Math.p ow((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
Oct 27 '08 #3
SAM <st************ *********@wanad oo.fr.invalidwr ites:
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

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Oct 27 '08 #4
SAM wrote on 27 okt 2008 in comp.lang.javas cript:
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().r eplace(/(\d\d\d)$/,'.$1')
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 28 '08 #5
In comp.lang.javas cript message <Xn************ ********@194.10 9.133.242>
, Tue, 28 Oct 2008 09:03:18, Evertjan. <ex************ **@interxnl.net >
posted:
>
var aNumber = 1.2345
var resultString = (aNumber*1000+. 5).toString().r eplace(/(\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.demo n.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)
Oct 28 '08 #6
On 2008-10-28 20:21, Dr J R Stockton wrote:
>>var aNumber = 1.2345
var resultString = (aNumber*1000+. 5).toString().r eplace(/(\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
Oct 29 '08 #7
On Oct 29, 1:27*pm, Conrad Lender <crlen...@yahoo .comwrote:
On 2008-10-28 20:21, Dr J R Stockton wrote:
>var aNumber = 1.2345
var resultString = (aNumber*1000+. 5).toString().r eplace(/(\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
Oct 29 '08 #8
On 2008-10-29 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
Oct 29 '08 #9
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="VBScr ipt">
<!--
Function SLL(cross,erd,s pokes,erd,osb)
SSL =Math.sqrt(Math .pow(((fdl/2*Math.sin((2*M ath.PI*cross)))/ (spokes/
2)),2)+Math.pow ((erd/2-((fdl/2)*Math.cos(2*M ath.PI*cross/ (spokes/
2)))),2)+Math.p ow((c2l+osb),2)-shd/2);
End Function
//-->
</script>

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

<script language="VBScr ipt">
<!--
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>
Oct 29 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
4590
by: VictorG | last post by:
Hello, I am new to JS and am trying to add some HTML into a JS function. So that when called the script as well as the HTML will be invoked. Is there some type of embed mechanism, sort of the reverse of embedding JS in an html page with the script tag. (which is what I am doing in this case) Is this even possible?
4
3432
by: frogman042 | last post by:
My daughter is playing around trying to learn JavaScript and she wrote a small program that prints out a message in increasing and decreasing font size and color changes. She is using document write and it works fine until she puts it into a function and then calls the function from a button with onClick. This also seems to work OK, with the exception that the page is cleared, and the curser changes (and stays) as an hourglass. In...
2
5410
by: Head In A Pan | last post by:
Hello! My JavaScripting is at novice level - and after completing a tutorial on a 'floating layer' I was proud that I got it working on Firefox, Safari, Camino & even IE5 for mac!!! But not IE7! The 'floating layer' does not float in IE7... it just sits up at the top of the screen - stuck in one place!! Explorer - why do you mock us?!!! ;( I have tried everything - but I am at a loss. I am thinking maybe it's some conflicting CSS or some...
2
3259
by: degroot.ryan | last post by:
Hey, I'm writing a script that will output a PMT similar to Excel. I've searched the net and I've found a code that works, except if a FutureValue is required. Here's what I found (can't remember where, sorry) function pmt_calc( intrest_rate , months , principal_value , fv ) { pmt = Math.round( (principal_value * intrest_rate) / (1 - Math.pow(1
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7409
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.