![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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 |
#3
| |||
| |||
|
|
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? |
#4
| |||
| |||
|
|
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. |
#5
| |||
| |||
|
|
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 |
#6
| |||
| |||
|
|
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 |
#7
| |||
| |||
|
|
var aNumber = 1.2345 var resultString = (aNumber*1000+.5).toString().replace(/(\d\d\d)$/,'.$1') |
#8
| |||
| |||
|
|
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. |
#9
| |||
| |||
|
|
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? |
#10
| |||
| |||
|
|
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% |
![]() |
| Thread Tools | |
| Display Modes | |
| |