Car payment estimation formula in javascript - javascript

The Formula looks like this:
(25000 x (.06 / 12)) / (1 - ((1 + (.06 / 12))^(-36))) = 760.548436
I have been attempting to convert this to javascript but with not much luck. If you put that above formula into google you will see the answer.
After many attempts and different methods, braking up the formula into different variables then dividing them, I haven't had any luck, when I came up with this, it got me the wrong answer:
var loan = 25000;
var rate = 6 / 100;
var term = 36;
var calculate = (loan * (rate / 12)) / (1 - ((1 + (rate / 12))^(-term)));
console.log(calculate);
Output was:
3.4722222222222223
And not 760.548436. Anyone have any ideas?

The caret is a bitwise operator. You want this formula instead:
calculate = (loan * (rate / 12)) / (1 - Math.pow(1 + (rate / 12), -term));
It gives you the answer you expect, 760.5484362888927

Related

How to use Math.float for a damage formula equation

I have found a guide for some damage formulas from FF9 and want to use them
in RMMV which uses Javascript.
Im bad at math and dont know how the Math.random and modulo
works or at least im doing something wrong.
I allways get 0 damage and im sure its because of wrong equation.
Since the Math.random gives a float from 0.00 to 1.00 I thought that would be the problem.
So I've tried to use a random number between 1 and 100 but that didnt helped.
Base = Spell Power - Mag Def
Bonus = Mag + Rnd MOD ([(Lvl + Mag) / 8] + 1)
Damage = Base * Bonus
SPELLPOWER - b.mdf * (a.mat + (Math.random() % ((a.level + a.mat) / 8) + 1))
16 - 2 * (16 + (Math.random() % ((1 + 16) / 8) + 1))
SPELLPOWER - b.mdf * (a.mat + ((Math.random() * (100 - 1) + 1) % ((a.level + a.mat) / 8) + 1))
16 - 2 * (16 + ((Math.random() * (100 - 1) + 1) % ((1 + 16) / 8) + 1))
Somehow this should actualy give a number higher then 0 with the stats i provide.
It was just some wrong set brackets which caused the trouble.
(SPELLPOWER - b.mdf) * (a.mat + Math.random() % (((a.level + a.mat) / 8) + 1))
This works perfectly fine now.

Getting to 16 Available Bits in Javascript

I recently came across a YouTube video that discussed handling GPS coordinates by using space filled curves. There was a formula provided to accomplish this so I've decided to try and replicate it, however I have not been able to figure out how to get to the same result using JavaScript.
The formula was as follows:
Scale Latitude and longitude to use 16 available bits each:
scaled_x = (-122.4012 + 180) / 360 * 2 ^ 16 // result = 10485
scale_y = (37.7839 + 90) / 180 * 2 ^ 16 // result = 46524
Video Reference
I've tries several ways of writing the formula and my results are far off from what I should be getting based on what was shown in the presentation. Either the presentation was inaccurate or I've not landed on the correct way of getting to this.
Here are some of my attempts, all fail.
Using Pow
base = Math.round((lat + 180) / 360);
scale = Math.pow(base * 2, 16); // Result = 0
Exact Formula
base = Math.round((lat + 180) / 360 * 2 ^ 16); // Result = 16
Inline Power
base = Math.round((lat + 180) / 360 * Math.pow(2, 16)); // Result = -22282
Does anyone know how this formula needs to be structured in JavaScript to get the expected outcome?
base = Math.round((lat + 180) / 360);
scale = Math.pow(base * 2, 16)
This doesn't work because you're rounding the value before multiplying it by 216 -- so it will round to either 0 or 1. This is not what you want.
base = Math.round((lat + 180) / 360 * 2 ^ 16);
In Javascript -- and many other languages -- ^ is used for bitwise XOR, not exponentiation. 2 ^ 16 is 18, not 65536.
base = Math.round((lat + 180) / 360 * Math.pow(2, 16));
This looks correct. The result you've quoted isn't right for lat = -122.4012, though -- did you leave out the + 180, perhaps?

Trying to write some maths from Wikipedia in Javascript (sun position computation)

I am trying to approximate the position of the sun in XYZ for a threejs project.
I am following the maths found here: http://en.wikipedia.org/wiki/Position_of_the_Sun
Following the above, I have written the following Javascript code:
var n = ((2440587.5 + (this.datemillis / 8.64E7)) - 2451545);
var L = 280.460 + 0.9856474 * n;
var g = 357.528 + 0.9856003 * n;
L = (L + 360) % 360;
g = (g + 360) % 60;
var lambda = L + 1.915 * Math.sin(g) + 0.0020 * Math.sin(2 * g);
var r = 1.00014 - 0.01671 * Math.cos(g) - 0.00014 * Math.cos(2 * g);
var e = 23.439 - 0.0000004 * n;
var x = (r * this.constants.EARTH_RADIUS * 2) * Math.cos(lambda);
var y = (r * this.constants.EARTH_RADIUS * 2) * Math.cos(e) * Math.sin(lambda);
var z = (r * this.constants.EARTH_RADIUS * 2) * Math.sin(e) * Math.sin(lambda);
this.datemillis is returned by the getMillisecond function of the Javascript date object. It is updated each frame so that time advances at about 1 hour every 2 seconds.
However something must not be correct as this does not produce the expected result. When I apply the computed x y z coordinates to my sun in my threejs project, I can see the sun rotate around the earth (sitting in 0,0,0) but at a very slow rate (rotating the earth in a few days instead of 24 hours).
I'm thinking it might have something to do with the angle calculations that I'm not doing correctly (degrees/radians?) but I'm not very good at maths so I don't really know what I'm doing so maybe I just misinterpreted the Wiki calculations.
If somebody could spot something obvious I'm doing wrong and help me fix this, would be greatly appreciated!
Thanks
EDIT: so my sun currently is not rotating around the earth in a continous way - it rotates clockwise/counterclockwise alternatively and sometimes jumps positions...
I suggest this to get the Julian Date, from Calculating Jday(Julian Day) in javascript
var today = Date();
var JD = Math.floor((today / 86400000) - (today.getTimezoneOffset()/1440) + 2440587.5);
Add to JD the desired amount of days and increment that value at the desired speed. Note that if you add 1 day each millisecond you'll get 1000 days per second, not 1 hour every 2 seconds.
JD += offset;
Then go on with the wikipedia recipe:
var n = JD - 2451545;
//...
To put L and g in the range 0-360 (you have an error here) use
L = L % 360 + ( L < 0 ? 360 : 0 );
g = g % 360 + ( g < 0 ? 360 : 0 );
The wikipedia formulas express angles in degrees. However JavaScript trigonometric functions cos and sin expect radians.
Just write a "degrees" version of them:
function cosD( deg ) {
return Math.cos( deg * Math.PI / 180.0 );
}
function sinD( deg ) {
return Math.sin( deg * Math.PI / 180.0 );
}
Then use sinD() and cosD() in subsequent calculations.
var r = 1.00014 - 0.01671 * cosD(g) - 0.00014 * cosD(2 * g);
var e = 23.439 - 0.0000004 * n;
var x = (r * this.constants.EARTH_RADIUS * 2) * cosD(lambda);
var y = (r * this.constants.EARTH_RADIUS * 2) * cosD(e) * sinD(lambda);
var z = (r * this.constants.EARTH_RADIUS * 2) * sinD(e) * sinD(lambda);
I cannot answer your question but I do know this is a solved problem in threejs. There is an example running in an architecture/engineering workflow on Github on this topic. The sun position code is here https://github.com/radio412/viewer/blob/gh-pages/sun-position.js
You can see it being tapped for a directional light in threejs at line 108 here: https://github.com/radio412/viewer/blob/gh-pages/va3c-viewer.js

Distance from long lat line segment wrong over long distances [duplicate]

This question already has answers here:
Calculate possible lines from points
(2 answers)
Closed 9 years ago.
I am calculating how far away a point is from a line segment on the earth.
My function seemed to work, but as i've increased the distances it's clear it's not working. I presume this is due to the curvature of the earth.
In my calculations Rome is shown as 5km from the line:
London, UK - 0km
Rome, Italy - 5km
Cyrene, Libya - 0km
But on Google Earth it's actually more like 61km
When I start going longer distances the calculations get even worse!
Rome, Italy - 0km
Mohenjo-daro, Pakistan - 0km
Istanbul, Turkey - 250km
I believe the problem is somewhere in the code here:
function distToSegment(lat1, lon1, lat2, lon2, lat3, lon3) {
var y = Math.sin(lon3 - lon1) * Math.cos(lat3);
var x = Math.cos(lat1) * Math.sin(lat3) - Math.sin(lat1) * Math.cos(lat3) * Math.cos(lat3 - lat1);
var bearing1 = radiansToDegrees(Math.atan2(y, x));
bearing1 = 360 - (bearing1 + 360 % 360);
var y2 = Math.sin(lon2 - lon1) * Math.cos(lat2);
var x2 = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lat2 - lat1);
var bearing2 = radiansToDegrees(Math.atan2(y2, x2));
bearing2 = 360 - (bearing2 + 360 % 360);
var lat1Rads = degreesToRadians(lat1);
var lat3Rads = degreesToRadians(lat3);
var dLon = degreesToRadians(lon3 - lon1);
var distanceAC = Math.acos(Math.sin(lat1Rads) * Math.sin(lat3Rads) + Math.cos(lat1Rads) * Math.cos(lat3Rads) * Math.cos(dLon)) * 6371;
var min_distance = Math.abs(Math.asin(Math.sin(distanceAC / 6371) * Math.sin(degreesToRadians(bearing1) - degreesToRadians(bearing2))) * 6371);
return min_distance;
}
Here is a working fiddle you can use to test:
http://jsfiddle.net/kmturley/cfg2D/3/
Any help to figure this one out would be appreciated!
This
bearing1 = 360 - (bearing1 + 360 % 360)
looks fishy to me. Do you mean
bearing1 = 360 - (bearing1 + 360) % 360
?
Likewise for bearing2.
% is a multiplicative operator and has higher precedence than +.

Getting a NaN error, can't figure out why

I have this code in JavaScript:
SolarPanels = parseInt(lRemainingWidth / (panel_width + ( 2 * lPanelInterSpace)));
and then I alert the SolarPanels value which gives NaN as output,
alert("SolarPanels "+SolarPanels);
This 1 line is a tiny part of a huge calculation, but my code seems to fail here,
with the use of alerts i've read out the values of SolarPanels, lRemainingWidth, panel_width and lPanelInterSpace
which are the following:
lRemainingwidth = 17.4.320227272727276
SolarPanels = 0
panel_width = 1.65
lPanelInterSpace = 0.02
I think it has to do with the 2 dots in lRemainingWidth, either way, I don't know how to fix it. Why the lRemainingWidth has 2 dots?
Update :
This is the part that calculates the lRemainingWidth :
if(HalforDouble === "Yes")
{
lRemainingWidth = (roof_ridge /2) + ((lRemainingHeight / Math.tan((lRoofEdgeDegrees * Math.PI) / 180)) - lRoofEdge);
}
else
{
lRemainingWidth = roof_ridge + (2 * ((lRemainingHeight / Math.tan((lRoofEdgeDegrees * Math.PI) / 180)) - lRoofEdge));
}
The values here are:
lRemainingWidth = 0
roof_ridge = 17
lRemainingHeight = 20.769000000000002
lRoofEdgeDegrees = 83.5169263071276
lRoofEdge = 0.2
Your problem is that you mix strings and numbers
Start with this code before any computation :
var roof_ridge = parseFloat(roof_ridge);
There might be other strings hidden in your code but we don't see them. Apply the same conversion on them.
lRemainingWidth = roof_ridge + (2 * ((lRemainingHeight / Math.tan((lRoofEdgeDegrees * Math.PI) / 180)) - lRoofEdge));
If roof_ridge is a string then the + does string concatenation instead of addition.
Change this to
lRemainingWidth = +roof_ridge + (2 * ((lRemainingHeight / Math.tan((lRoofEdgeDegrees * Math.PI) / 180)) - lRoofEdge));
The prefix + operator in +roof_ridge coerces its argument to a number.
It seems like a cornerstone of the issue in roof_ridge variable. This variable is instance of String class, not a number. So, when you code go to this line:
lRemainingWidth = roof_ridge + (2 * ((lRemainingHeight / Math.tan((lRoofEdgeDegrees * Math.PI) / 180)) - lRoofEdge));
the next calculation is done:
'17' + whatever_float_value = got string concatenation instead of number's sum.
To fix this just add:
lRemainingWidth = parseFloat(roof_ridge) + (2 * ((lRemainingHeight / Math.tan((lRoofEdgeDegrees * Math.PI) / 180)) - lRoofEdge));

Categories

Resources