Python/Javascript number precision [duplicate] - javascript

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
Probably this question is already existed somewhere but can't find my specific issue.
I have a two numbers that I want to add and I don't want to use round() function since I am working with money.
1.70 + 1.44 = 3.14 //Returns 3.1399999999999997
1.70 + 1.45 = 3.15 //Returns the correct answer
1.70 + 1.37 = 3.07 //Returns 3.0700000000000003
I tried it in Javascript but still the same issue.

Since you're working with money, always use the decimal.Decimal class in Python.

Use parseFloat to precision :
parseFloat(number).toFixed(precision);
E.g.
parseFloat(1.70 + 1.37).toFixed(2); // RETURN 3.07

one way you can achieve this is to multiply the values by (10 * precision) that you need and then divide the result by (10 * precision).
((1.70 * 100) + (1.37 * 100))/100 = 3.07
((1.70 * 100) + (1.45 * 100))/100 = 3.15
((1.70 * 100) + (1.44 * 100))/100 = 3.14
you can add a method to do this for you to keep it simple.

Related

Math.floor () to round down to nearest 0.5 in javascript [duplicate]

This question already has answers here:
Javascript roundoff number to nearest 0.5
(10 answers)
Closed 3 years ago.
I have a function from excel that I use: =FLOOR(value,0.5)
With this function, I round DOWN to the nearest half value. For example:
10.55 becomes 10.5
10.99 becomes 10.5
10.2 becomes 10
Etc.
Is there an equivalent in javascript? I know that Math.floor () will round down to the nearest integer. However, does anyone know of a good way to round down to the nearest 0.5 instead?
Thank you in advance
Here's your function:
function slipFloor(num){
let f = Math.floor(num);
if(num-f < 0.5){
return f;
}
return f+0.5;
}
console.log(slipFloor(10.55));
console.log(slipFloor(10.99));
console.log(slipFloor(10.2));

Excel formula calculations into JavaScript [duplicate]

This question already has answers here:
JavaScript exponents
(8 answers)
Closed 4 years ago.
I have a formula into my excel sheet, now I am making that formula using JavaScript. I made a code exact like written on excel sheet but in JavaScript I am getting different result, I mean wrong result.
Excel formula
= H4*((((1+H7)^H8-1)/H7)*(1+H7))
JavaScript
var contribute = 1000;
var cum_rate = 0.001666667;
var num_periods = 480;
var fvc = contribute * ((((1 + cum_rate) ^ num_periods - 1) / cum_rate) * (1 + cum_rate));
console.log(fvc);
Result of this calculations should be 735659.68 but here I am getting wrong result, can you guys help me out what I am doing wrong here?
The karat doesn't mean exponent. You need to use Math.pow(base, exp) to evaluate the expression correctly:
var fvc = contribute*( ((Math.pow( (1+cum_rate), num_periods ) - 1)/cum_rate)*(1+cum_rate) )

Subtraction problems in JavaScript [duplicate]

This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 6 years ago.
If one subtracts 5 from 8.8, the actual result is: 3.8 but in my code it gives 3.8000000007. Why it this? Could anyone kindly provide a valuable insight? Thanks in advance.
It's working :
var p = 8.8 - 5;
console.log(p.toFixed(1));
I think there is nothing wrong in using "toFixed()". Why you are looking for an answer without using it?
Nervertheless you could do it the mathematical way:
var result = (8.8 * 10 - 5 * 10) / 10;
console.log(result);
The Javascript Engine is only trying to help you out. If it finds a Floating Point Number amongst the Numbers supplied in your Arithmetic Operations, it will automatically parse the Result to a Floating Point Number as well - (with the highest possible Precision).
Therefore, to get around this Eager but Unsolicited Help of the Javascript Engine; you'd need to manually call: toFixed() on the Resulting Value like so:
var p = 8.8 - 5;
console.log( p.toFixed(1) ); //<== 3.8
Alternatively, You may extend the Number Prototype and add your Unique Functionality if that is what you desire. However, this is not at all advised!!!
<script type="text/javascript">
Number.prototype.precise = function(){
return this.toFixed(1);
};
Number.prototype.floatSubtract = function(num){
return (this - num).toFixed(1);
};
var p1 = (8.8 - 5).precise();
var p2 = 8.8.floatSubtract(5);
console.log(p1); //< 3.8
console.log(p2); //< 3.8
</script>
Try this. I think this will be the solution.
<!doctype HTML>
<html>
<head>
<script>
num = 8.8 - 5;
console.log(Math.round(num * 100) / 100)
</script>
</head>
</html>
In the below picture, Calculation part is there. Kindly Check it.

Confusion over unexpected Math.cos behavior in Javascript [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
The cosine of 90 degrees is 0.
I understand Javascript's Math.cos takes radians, so I've tried:
Math.cos(90 * Math.PI / 180)
Why does this yield 6.123233995736766e-17 instead of 0?
6.123233995736766e-17 is essentially 0. It's 0.00000000000000006123233995736766. That kind of minor error is completely normal when working with IEEE floating point numbers.
The solution is to never compare numbers exactly, but compare if they are within a range that you expect. EG.
var result = Math.cos(90 * Math.PI / 180);
if ( Math.abs( result - 0 ) < 1e-16 ) {
// Test passed, result is effectively 0
}
The - 0 does nothing, but more generally - x is how you would compare to x. You are trying to compare to 0, so I used - 0, but you could leave that off and get the same result.

How to get x of 2^x=8000? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What's the opposite of JavaScript's Math.pow?
2^x=i
Given i, how can we calculate x using Javascript?
You want to take the logarithm of 8000. JS has the Math.log function which uses base e, you want base 2 so you can write Math.log(8000) / Math.log(2) to get the logarithm of 8000 base 2, which is equal to x.
You need the logarithm from the Math object. It does not provide a base 2 log so do the conversion:
var x = Math.log(8000) / Math.log(2);
Reference to the javascript Math object.
In the more general case we calculate 2^x = i this way:
var i; // Some number
var x = Math.log(i) / Math.log(2);

Categories