Convert repeating decimal to fraction - javascript

I am using a button on a JavaScript scientific calculator to convert decimal to fractions via this code:
$('#button-frac').click(function(){
var factor;
// Finds the highest common factor of 2 numbers
function highestCommonFactor() {
for (factor = numerator; factor > 0; factor--) {
if ((numerator % factor == 0) && (denominator % factor == 0)) {
return factor;
}
}
}
// Enter a decimal to convert to a fraction
var decimal = this.form.display.value;
// Split the decimal
var decimalArray = decimal.split(".");
var leftDecimalPart = decimalArray[0];
var rightDecimalPart = decimalArray[1];
// Save decimal part only for later use
var decimalOnly = "0." + rightDecimalPart;
// Find the decimal multiplier
var multiplier = "1";
for (var i = 0; i < rightDecimalPart.length; i++) {
multiplier += "0";
}
// Create numerator by multiplying the multiplier and decimal part together
var numerator = Number(multiplier) * Number(decimalOnly);
var denominator = multiplier;
// Find the highest common factor for the numerator and denominator
highestCommonFactor();
// Simplify the fraction by dividing the numerator and denominator by the factor
var numerator = Number(numerator) / Number(factor);
var denominator = Number(denominator) / Number(factor);
// Output as a mixed number fraction (depending on input)
var mixedNumber = leftDecimalPart + " " + numerator + "/" + denominator;
// Output as a proper fraction or improper fraction (depending on input)
var numerator = numerator + (leftDecimalPart * denominator);
var fraction = numerator + "/" + denominator;
// Display solution in input #disp
$('#disp').val(fraction);
});
This works well, but if the decimal is non-terminating and repeating the script crashes. Any idea how I might remedy this problem? Perhaps there's a way to check if a decimal repeats and to determine the length of the string that repeats, then take that string and express it over a number with equal number of digits, all 9s? Being relatively new to JavaScript, I am at a loss.

Use this and round the decimal after at most 20 repeated chars.
Math.fraction=function(x){
return x?+x?x.toString().includes(".")?x.toString().replace(".","")/(function(a,b){return b?arguments.callee(b,a%b):a;})(x.toString().replace(".",""),"1"+"0".repeat(x.toString().split(".")[1].length))+"/"+("1"+"0".repeat(x.toString().split(".")[1].length))/(function(a,b){return b?arguments.callee(b,a%b):a;})(x.toString().replace(".",""),"1"+"0".repeat(x.toString().split(".")[1].length)):x+"/1":NaN:void 0;
}
Call it with Math.fraction(2.56)
It will:
return NaN if the input is not a number
return undefined if the input is undefined
reduce the fraction
return a string (use Math.fraction(2.56).split("/") for an array containing the numerator and denominator)
Please note that this uses the deprecated arguments.callee, and thus may be incompatible in some browsers.
Test it here

Related

Javascript round / floor / toFixed on decimals

I am having an issue with how javascript is dividing and rounding the number.
I have two float , 0.11 and 0.12
I want to calculate the mid of these two numbers and round it to the nearest highest value with 2 decimal price.
For example, if I do this on Calculator
0.11+0.12 / 2 = 0.115, and I need to round it to 0.12 as it is mid or above mid.
If I do this with Javascript, I am not getting an accurate number
Example,
var high = parseFloat(0.12);
var low = parseFloat(0.11);
var mid = (high + low) / 2;
document.getElementById("demo1").innerHTML = mid;
document.getElementById("demo2").innerHTML = mid.toFixed(2);
var another = mid.toFixed(3);
document.getElementById("demo3").innerHTML =another;
var last = Math.floor(another)
document.getElementById("demo4").innerHTML =last;
http://jsfiddle.net/gzqwbp6c/9/
Any input would be appreciated.
As the 0.11499999999999999 shows, the result is very slightly less than 0.115. This is because 0.11 and 0.12 cannot be represented with perfect accuracy using floating-point-numbers.
When you don't want to deal with floating-point-error, it's often easier to work with integers directly. Small integers are represented exactly by floating point numbers.
You can multiply by 100 before, and round, to ensure your numbers are integers, and only divide after you get your final result:
var a = Math.round(100 * parseFloat("0.12")) // 12
var b = Math.round(100 * parseFloat("0.11")) // 11
var mid = (a + b) / 2 // 11.5.
// 0.5 can be represented exactly in floating point for small numbers.
var midRound = (Math.round(mid) / 100).toFixed(2) // "0.12"
Need to multiply (workout on int part, i.e. find mid, and divide to reconvert to origin):
function myMid(high,low, precision){
var precision=2
var convFactor = Math.pow(10,precision);
return
(Math.round((low*convFactor+high*convFactor)/2)/convFactor).toFixed(precision);
}
Float is not precise, you cant rely on that, you'll have unexpected results.
everything *100 to prevent inaccuracies
.toFixed() does the rounding
var a = 0.11;
var b = 0.12;
c = parseFloat((((a*100) + (b*100))/200).toFixed(2));
console.log(c);

toFixed method without rounding to five digit

I have a number var x = 2.305185185185195;
x = x.toFixed(5);
x = 2.30519 but I require this without rounding i.e. 2.30518
I read some thread with two decimal places but could not find for five decimal places.
Any help would be appreciated.
You can use an apropriate factor and floor it and return the result of the division.
Basically this solution moves the point to the left with a factor of 10^d and gets an integer of that and divided the value with the former factor to get the right digits.
function getFlooredFixed(v, d) {
return (Math.floor(v * Math.pow(10, d)) / Math.pow(10, d)).toFixed(d);
}
var x = 2.305185185185195;
document.write(getFlooredFixed(x, 5));
If you need only a "part" of a number with a floating point without rounding, you can just "cut" it:
function cutNumber(number, digitsAfterDot) {
const str = `${number}`;
return str.slice(0, str.indexOf('.') + digitsAfterDot + 1);
}
const x = 2.305185185185195;
console.log(cutNumber(x, 5)); // 2.30518
This method is fast (https://jsfiddle.net/93m8akzo/1/) and its execution time doesn't depend on number or digitsAfterDot values.
You can also "play around" with both functions in a given fiddle for a better understanding of what they do.
You can read more about slice() method here - MDN documentation
NOTE This function is only an example, don't use it in production applications.
You should definitely add input values validation and errors handling!
The Math.trunc() function returns the integer part of a number by
removing any fractional digits
So you can multiply the number by 10^n where n is the desired number of precision, truncate the decimal part using Math.trunc(), divide by the same number (10^n) and apply toFixed() to format it (in order to get the form of 2.30 instead of 2.3 for example)
var x = 2.305185185185195;
console.log((Math.trunc(x*100000)/100000).toFixed(5));
I have sorted it out by adding a small amount if the decimal is 5, then rounding as usual:
function(value, decimals) {
var decimals = decimals || 2;
if( isNaN(value) ){ return 0; }
var decimalPart = value.toString().trim().split('.').pop(),
extra = decimalPart.substr(decimals, decimalPart.length - decimals);
if( extra == '5' &&
decimalPart.length > decimals
){
value = parseFloat(value) + (1 / ( Math.pow(10, decimals + 5) ) );
}
return Number( parseFloat( value ).toFixed( decimals ) );
}

Math.abs() Limit the amount of deimals

I have scoured the internet and I haven't found a solution that really works for me, yet.
var tv = Length * Type;
if (tv < 0)
{
cForm.voltage.value = "-" + Math.abs(tv) + " V";
}
else...
Some of the calculations with these two numbers come out to about the 15th decimal for some reason. I would like to limit the decimal amount that is returned, and NOT allow the number to round up or down. On a calculator it only comes out to about the third decimal, but Math.abs() brings it too far out.
.toFixed() Doesn't work for me because if the number only has 2 decimals it will add additional zeros at the end. I only want to display up to the fourth if it is calculated.
Just expanding on #goto-0 s comment, with the correct # of decimal places.
var tv = Length * Type;
if (tv < 0)
{
cForm.voltage.value = "-" + (Math.round(Math.abs(tv) * 10000) / 10000) + " V";
}
else...
Here's the implementation as a function that truncates the extra decimal places. If you want to round the output you could just use Number.toPrecision().
function toFixedDecimals(num, maxDecimals) {
var multiplier = Math.pow(10, maxDecimals);
return Math.floor(num * multiplier) / multiplier
}
console.log(toFixedDecimals(0.123456789, 4));
console.log(toFixedDecimals(100, 4));
console.log(toFixedDecimals(100.12, 4));
I'm sure its not the most efficient approach but it is pretty brainless -
grab your result
split it into an array based on the decimal point
then trim the decimal part to two digits (or however many you would like).
concat the pieces back together
Sorry for the long variable names - just trying to make it clear what was happening : )
// your starting number - can be whatever you'd like
var number = 145.3928523;
// convert number to string
var number_in_string_form = String(number);
// split the number in an array based on the decimal point
var result = number_in_string_form.split(".");
// this is just to show you what values you end up where in the array
var digit = result[0];
var decimal = result[1];
// trim the decimal lenght to whatever you would like
// starting at the index 0 , take the next 2 characters
decimal = decimal.substr(0, 2);
// concat the digit with the decimal - dont forget the decimal point!
var finished_value = Number(digit + "." + decimal);
In this case the finished_value would = 145.39

How do I get the quotient as int and remainder as a floating point in JavaScript

On my calculator when I do 18/7 I get 2.5714285714285714285714285714286.
From my super limited Math skills 2 is the quotient and .5714285714285714285714285714286 is the remainder.
How can I model this in JavaScript?
Thanks!
var floatingPointPart = (18/7) % 1;
var integerPart = Math.floor(18/7);
Math.floor has the problem of rounding of the result to wrong direction in case of negative number it would be better to use bitwise operation to obtain interger quotients.
var quot = ~~(num/num1)
Hopefully this works for you!
var rawQuotient = 18/7;
var remainder = rawQuotient % 1;
var quotient = rawQuotient - remainder;
In most mathematics, there's no real need for distinction between the fractional portion and the whole portion, especially given that the whole thing can be expressed as a fraction (18/7ths), as a real number, or as a mix of integers and fractions (2 and 4/7ths).
When you get into programming or engineering, or some other derivative, you have definite uses for the separation; but the quotient is really the whole thing, integer and fraction, however you choose to represent that.
We can use simple mathematics to get answer using only / & % operator.
Consider 'num1' as first value & 'num2' as second value. Then :
var Quotient = (num1 - (num1 % num2)) / num2;
var FloatingPoint = (num1 % num2) / num2;
2 is the quotient (or integer part of the result), 4 is the remainder, and 4/7 is the floating point part of the result, which the OP is requesting.
var result = (18/7);
var integerPart = Math.floor(result);
var floatingPointPart = result - integerPart;
integerPart and floatingPointPart are the requested values.
var decimals = float - (float | 0);
In the decimal number 2.5714285714285716,
the quotient, which also is the whole part, is 2
and the decimal/fractional part is 0.5714285714285716 or 0.5714285714285716
but actually, the remainder is 4.000000000000002 or 4
let decimal = 18 / 7;
let quotient = ~~decimal;
let dec_remainder = decimal - quotient;
let dec_remainder0 = decimal % 1;
let remainder = dec_remainder * 7;
let remainder0 = 18 % 7;
document.body.innerHTML = "In the decimal number "+decimal+",<br/>
the quotient, which also is the whole part, is "+quotient+"<br/>
and the decimal/fractional part is "+dec_remainder+ " or " + dec_remainder0 + "<br/>
but actually, the remainder is "+remainder+" or "+remainder0 ;
<!DOCTYPE html>
<html>
<body>
</body>
</html>
You can determine the quotient in several ways.
Actually, 2 is the quotient and 4/18 is the remainder.
Math.divideby= function(d, dby){
var q= Math.floor(d/dby), r= d-(q*dby);
return r== 0? q:q+' and '+r+'/'+d;
}
Math.divideby(18,7)
/* returned value: (String) */
2 and 4/18

Calculating the maximum value for a decimal using scale and precision

I am working on a JavaScript function that takes two values: precision of a decimal value & scale of a decimal value.
This function should calculate the maximum value that can be stored in a decimal of that size.
For example: a decimal with a precision of 5 and a scale of 3 would have a maximum value of 99.999.
What I have does the job, but it's not elegant. Can anyone think of something more clever?
Also, please forgive the use of this weird version of Hungarian notation.
function maxDecimalValue(pintPrecision, pintScale) {
/* the maximum integers for a decimal is equal to the precision - the scale.
The maximum number of decimal places is equal to the scale.
For example, a decimal(5,3) would have a max value of 99.999
*/
// There's got to be a more elegant way to do this...
var intMaxInts = (pintPrecision- pintScale);
var intMaxDecs = pintScale;
var intCount;
var strMaxValue = "";
// build the max number. Start with the integers.
if (intMaxInts == 0) strMaxValue = "0";
for (intCount = 1; intCount <= intMaxInts; intCount++) {
strMaxValue += "9";
}
// add the values in the decimal place
if (intMaxDecs > 0) {
strMaxValue += ".";
for (intCount = 1; intCount <= intMaxDecs; intCount++) {
strMaxValue += "9";
}
}
return parseFloat(strMaxValue);
}
Haven't tested it:
function maxDecimalValue(precision, scale) {
return Math.pow(10,precision-scale) - Math.pow(10,-scale);
}
precision must be positive
maxDecimalValue(5,3) = 10^(5-3) - 10^-3 = 100 - 1/1000 = 99.999
maxDecimalValue(1,0) = 10^1 - 10^0 = 10 - 1 = 9
maxDecimalValue(1,-1) = 10^(1+1) - 10^1 = 100 - 10 = 90
maxDecimalValue(2,-3) = 10^(2+3) - 10^3 = 100000 - 1000 = 99000
What about
function maxDecimalValue(pintPrecision, pintScale)
{
var result = "";
for(var i = 0; i < pintPrecision; ++i)
{
if(i == (pintPrecision - pintScale)
{
result += ".";
}
result += "9";
}
return parseFloat(result);
}
Check it out here
I would do something along the lines of ((10 * pintPrecision) - 1) + "." + ((10 * pintScale) - 1)
Although pow(10,precision-scale) - pow(10,-scale) is the right formula, you will need to calculate it with decimal type instead of float.
For example, if precision=4 and scale=5, you will get 0.09999000000000001 if it's calculated with float.
Therefore, in Python, you can do something like:
from decimal import Decimal
def calculate_decimal_range(precision: int, scale: int) -> Decimal:
precision, scale = Decimal(precision), Decimal(scale)
return 10**(precision-scale) - 10**-scale

Categories

Resources