In JavaScript, the Math.fround() method is used to return the nearest
single precision float representation of a given number. Single
precision float representation is a method of storing floating point
numbers in a binary format that uses less space than double precision.
The Math.fround() method is useful in situations where precision is
not as important as storage space, such as in video and audio
processing.
In this article, we will discuss briefly about float representation in
JavaScript and give examples of how to use the Math.fround() method.
Float representation in JavaScript
JavaScript uses the IEEE 754 standard to represent floating point
numbers. The standard defines two formats: single precision and double
precision. Single precision uses 32 bits to represent a number, while
double precision uses 64 bits. Single precision can represent a range of
values from -3.4028235 × 10^38 to 3.4028235 × 10^38, with a
precision of about 7 decimal digits.
Using the Math.fround() method in JavaScript
The Math.fround() method takes a single argument, which can be any
number. It returns the nearest single precision float representation of
that number. Here are a few examples:
The syntax for Math.fround() method goes thus
Math.fround(num);
where num is the number we want to approximate to the nearest single
precision float representation.
Here are some quick examples of using the Math.fround() method
console.log(Math.fround(1.337));
console.log(Math.fround(1.5));
console.log(Math.fround(NaN));
Output
1.3370000123977661
1.5
NaN
In this example, we pass three numbers to the Math.fround() method.
The first number, 1.337, is not a perfect binary representation, so the
method returns the nearest single precision float representation of that
number. The second number, 1.5, is a perfect binary representation, so
the method returns the exact same number. The third number, NaN, is a
special value that represents “Not a Number”. When passed to the
Math.fround() method, it returns NaN.
We can apply the Math.fround() method across an array of numbers.
const numbers = [1.337, 2.718, 3.141, 5.789];
const floatNumbers = numbers.map(Math.fround);
console.log(floatNumbers);
Output
[
1.3370000123977661,
2.7179999351501465,
3.1410000324249268,
5.789000034332275
]
In this example, we have an array of numbers. We use the Array.map()
method to apply the Math.fround() method to each number in the array,
and return an array of the single precision float representations of
those numbers.
Example 1: Typical 32-bit representation
If we need floating-point values or decimal values in 32-bit, we can
simply apply the Math.fround() method on the value.
const num1 = 45.1019;
const num2 = 672.1;
console.log(Math.fround(num1));
console.log(Math.fround(num2));
Output
45.101898193359375
672.0999755859375
In this example, Math.fround() is used to return the nearest
single-precision float representation of a given number.
Example 2: Implementing a rounding function
Just as with other rounding methods, we can implement a special rounding function with higher precision where we define the number of rounding factors - decimal places - we want.
function sRound(num, precision) {
const factor = Math.pow(10, precision);
return Math.fround(Math.round(num * factor)) / factor;
}
console.log(sRound(3.14159, 2));
console.log(sRound(42.1, 1));
Output
3.14
42.1
In this example, Math.fround() is used to implement a higher precision
rounding function that can round a number to a specified number of
decimal places. The Math.fround() method is used to ensure that the
result remains a single-precision float.
Example 3: Calculating square roots
Here, we can calculate the square roots using the divide and conquer
approach alongside the Math.fround() method.
function squareRoot(num) {
let guess = num / 2;
let newGuess;
do {
newGuess = (guess + num / guess) / 2;
guess = Math.fround(newGuess);
} while (guess !== newGuess);
return guess;
}
console.log(squareRoot(2));
console.log(squareRoot(3));
console.log(squareRoot(5));
console.log(squareRoot(4));
Output
1.5
1.75
2.25
2
The Math.fround() method is used to ensure that the intermediate
guesses are single-precision floats, which can be more efficient for
large calculations.
Example 4: Calculating the average of a large array of numbers
const numbers = Array.from({ length: 1000000 }, () => Math.random() * 100);
const average = Math.fround(
numbers.reduce((sum, num) => sum + num, 0) / numbers.length
);
console.log(average);
Output
49.97899627685547
In this example, we create an array of 1 million random numbers between
0 and 100 using Array.from() and the Math.random() method. We then
calculate the sum of the array using the Array.reduce() method and
divide it by the length of the array to get the average. Finally, we use
the Math.fround() method to get the single precision float
representation of the average. This can be useful when working with
large arrays of numbers where memory usage is a concern.
Example 5: Financial Calculations
Math.fround() can be used in financial calculations to round off
decimal numbers to their nearest representable floating-point value. For
example, if you have a float value of 0.456 and you want to round it off
to its nearest representable floating-point value, you can use
Math.fround().
// Given a float value of 0.456, round it off to its nearest representable floating-point value
let floatVal = 0.456;
let roundedVal = Math.fround(floatVal);
console.log(roundedVal); // Outputs 0.4560000002384186
Example 6: Physics Calculations
Math.fround() can be used in physics calculations that involve large
or small numbers. For example, if you are calculating the speed of light
and end up with a very small number that is difficult to work with, you
can use Math.fround() to round it off.
// Given the speed of light is 299792458 m/s, calculate its value in miles per hour and round it off to the nearest representable floating-point value
let speedOfLightMps = 299792458;
let speedOfLightMph = (speedOfLightMps * 3600) / 1609.344;
let roundedSpeedOfLightMph = Math.fround(speedOfLightMph);
console.log(roundedSpeedOfLightMph); // Outputs 670616629.749505
Example 7: Using with Audio Processing
Math.fround() can be used in audio processing applications to ensure
that calculations are performed at the highest possible precision. For
example, if you are working with audio signals and need to ensure that
the calculations are performed accurately, you can use Math.fround().
// Given an audio signal with a frequency of 100 Hz and sample rate of 44100 Hz, calculate the number of samples per period and round it off to the nearest representable floating-point value
let frequency = 100;
let sampleRate = 44100;
let samplesPerPeriod = sampleRate / frequency;
let roundedSamplesPerPeriod = Math.fround(samplesPerPeriod);
console.log(roundedSamplesPerPeriod); // Outputs 441
Summary
The Math.fround() method in JavaScript is used to return the nearest
single precision float representation of a given number. Single
precision float representation uses less space than double precision,
but has lower precision. The method is useful in situations where
precision is not as important as storage space, such as in video and
audio processing.
References
Math.fround() - JavaScript | MDN
(mozilla.org)
Array.prototype.reduce() -
JavaScript | MDN (mozilla.org)

![JavaScript Math.fround() Examples [In-Depth Tutorial]](/javascript-math-fround/javascript-math-fround.jpg)