473,782 Members | 2,458 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
17 3924
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.
Oct 29 '08 #11
On Oct 28, 2:03*pm, "Evertjan." <exjxw.hannivo. ..@interxnl.net wrote:
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')
var aNumber = 1.2345678;
var resultString = (aNumber*1000+. 5).toString().r eplace(/(\d\d\d)$/,'.
$1');
alert(resultStr ing); // 1235.0.678

../sasuke
Oct 30 '08 #12
sasuke wrote on 30 okt 2008 in comp.lang.javas cript:
>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')

var aNumber = 1.2345678;
var resultString = (aNumber*1000+. 5).toString().r eplace(/(\d\d\d)$/,'.
$1');
alert(resultStr ing); // 1235.0.678
You are right, should be[, only where the absolute value is above 1]:

var aNumber = 1.2345678;
var resultString =
Math.floor(aNum ber*1000+.5).to String().replac e(/(\d\d\d)$/,'.$1');
alert(resultStr ing); // 1.235


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 30 '08 #13
Evertjan. wrote on 30 okt 2008 in comp.lang.javas cript:
sasuke wrote on 30 okt 2008 in comp.lang.javas cript:
>>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')

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

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

var aNumber = 1.2345678;
var resultString =
Math.floor(aNum ber*1000+.5).to String().replac e(/(\d\d\d)$/,'.$1');
alert(resultStr ing); // 1.235
Wow, not that easy in regex, try:

<script type='text/javascript'>

function round3dec(x) {
x = Math.floor(x*10 00+.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.23 45678) );
alert( round3dec(-21.2345678) );
alert( round3dec(0.002 5555) );
alert( round3dec(-0.0025555) );

</script>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 30 '08 #14
On 2008-10-30 21:59, Evertjan. wrote:
Wow, not that easy in regex, try:

<script type='text/javascript'>

function round3dec(x) {
x = Math.floor(x*10 00+.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.23 45678) );
alert( round3dec(-21.2345678) );
alert( round3dec(0.002 5555) );
alert( round3dec(-0.0025555) );

</script>
(10000000000000 00000).toFixed( 3)
-100000000000000 0000.000

round3dec(10000 00000000000000)
-1e+21.00001e+21

:-)
- Conrad
Oct 30 '08 #15
On Oct 31, 2:57*am, Conrad Lender <crlen...@yahoo .comwrote:
On 2008-10-30 21:59, Evertjan. wrote:
Wow, not that easy in regex, try:
<script type='text/javascript'>
function round3dec(x) {
* x = Math.floor(x*10 00+.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.23 45678) );
alert( round3dec(-21.2345678) );
alert( round3dec(0.002 5555) );
alert( round3dec(-0.0025555) );
</script>

(10000000000000 00000).toFixed( 3)
* -100000000000000 0000.000

round3dec(10000 00000000000000)
* -1e+21.00001e+21

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

../sasuke
Oct 31 '08 #16
sasuke wrote on 31 okt 2008 in comp.lang.javas cript:
On Oct 31, 2:57*am, Conrad Lender <crlen...@yahoo .comwrote:
>On 2008-10-30 21:59, Evertjan. wrote:
Wow, not that easy in regex, try:
<script type='text/javascript'>
function round3dec(x) {
* x = Math.floor(x*10 00+.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.23 45678) );
alert( round3dec(-21.2345678) );
alert( round3dec(0.002 5555) );
alert( round3dec(-0.0025555) );
</script>

(1000000000000 000000).toFixed (3)
* -100000000000000 0000.000

round3dec(1000 000000000000000 )
* -1e+21.00001e+21

:-)

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.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 31 '08 #17
In comp.lang.javas cript message <ca9917a8-230f-48df-a016-1efe5081a3fa@i1
8g2000prf.googl egroups.com>, Fri, 31 Oct 2008 07:15:36, sasuke
<da*********@gm ail.composted:
>
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.

--
(c) John Stockton, near London. *@merlyn.demon. co.uk/?.**********@ph ysics.org
Web <URL:http://www.merlyn.demo n.co.uk/- FAQish topics, acronyms, & links.
Correct <= 4-line sig. separator as above, a line precisely "-- " (SoRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SoRFC1036)
Oct 31 '08 #18

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

Similar topics

10
4591
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
3433
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
3260
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
10313
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...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
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
7494
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
6735
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
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.