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
  #11  
Old   
gimme_this_gimme_that@yahoo.com
 
Posts: n/a

Default Re: JavaScript Math vs Excel - 10-29-2008 , 06:44 PM






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>

Reply With Quote
  #12  
Old   
gimme_this_gimme_that@yahoo.com
 
Posts: n/a

Default Re: JavaScript Math vs Excel - 10-29-2008 , 06:45 PM






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.

Reply With Quote
  #13  
Old   
sasuke
 
Posts: n/a

Default Re: JavaScript Math vs Excel - 10-30-2008 , 01:57 AM



On Oct 28, 2:03*pm, "Evertjan." <exjxw.hannivo... (AT) interxnl (DOT) net> wrote:
Quote:
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


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

Default Re: JavaScript Math vs Excel - 10-30-2008 , 04:23 PM



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

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


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

Default Re: JavaScript Math vs Excel - 10-30-2008 , 04:59 PM



Evertjan. wrote on 30 okt 2008 in comp.lang.javascript:

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


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

Default Re: JavaScript Math vs Excel - 10-30-2008 , 05:57 PM



On 2008-10-30 21:59, Evertjan. wrote:
Quote:
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


Reply With Quote
  #17  
Old   
sasuke
 
Posts: n/a

Default Re: JavaScript Math vs Excel - 10-31-2008 , 10:15 AM



On Oct 31, 2:57*am, Conrad Lender <crlen... (AT) yahoo (DOT) com> wrote:
Quote:
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*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 built-in functions
whenever possible. ;-)

../sasuke


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

Default Re: JavaScript Math vs Excel - 10-31-2008 , 02:45 PM



In comp.lang.javascript message <ca9917a8-230f-48df-a016-1efe5081a3fa@i1
8g2000prf.googlegroups.com>, Fri, 31 Oct 2008 07:15:36, sasuke
<database666 (AT) gmail (DOT) com> posted:
Quote:
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/?.?.Stockton (AT) physics (DOT) org
Web <URL:http://www.merlyn.demon.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)


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

Default Re: JavaScript Math vs Excel - 10-31-2008 , 05:55 PM



sasuke wrote on 31 okt 2008 in comp.lang.javascript:

Quote:
On Oct 31, 2:57*am, Conrad Lender <crlen... (AT) yahoo (DOT) com> wrote:
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*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 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)


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.