Add 0.1 ten times in JavaScript and you get 0.9999999999999999, not 1! In this video, learn Math.sumPrecise(), the new ES2026 JavaScript feature that adds numbers accurately and fixes the floating-point error that reduce() and for loops create.
Math.sumPrecise in Hindi: is video mein hum dekhenge ki reduce() se sum karne mein rounding error kyun aata hai, aur JavaScript ka naya Math.sumPrecise() method isko kaise fix karta hai — simple examples ke saath.
🔥 What you'll learn: ✅ Why 10 × 0.1 gives 0.9999999999999999 with reduce() ✅ What Math.sumPrecise() is and how it works ✅ reduce() vs Math.sumPrecise() — step-by-step rounding error explained ✅ Real use case: e-commerce cart total and billing ✅ How to calculate an accurate average in JavaScript ✅ Big number bug: why [1e20, 0.1, -1e20] gives 0 with reduce() ✅ What Math.sumPrecise() does NOT fix (the 0.1 + 0.2 problem) ✅ Is reduce() still useful? When to use Math.max, Object.groupBy and Object.fromEntries ✅ Browser and Node.js support + core-js polyfill
⌛TIMELINE⏳ 0:00 10 Paise × 10 = ? A Simple Math Question 0:20 Why JavaScript reduce() Gives the Wrong Sum (0.9999999999999999) 0:50 Math.sumPrecise() – New ES2026 Method to Fix Floating-Point Errors 2:02 Node.js Live Online Class – Enroll Now 2:30 How Math.sumPrecise() Gives the Correct Answer 3:01 Animation: reduce() vs Math.sumPrecise() – How Each Works Step by Step 3:40 Example 2: Math.sumPrecise() Wins Again (Big Number Bug) 5:00 Real-World Use Cases of Math.sumPrecise() – Cart Total & Billing 6:18 Accurate Average in JavaScript with Math.sumPrecise() 7:30 reduce() vs Math.sumPrecise() – Which One Is Best? 7:55 When Both Fail – Math.sumPrecise() Limitations (0.1 + 0.2) 9:40 When reduce() and Math.sumPrecise() Give the Same Result 11:10 Outro – Next ES2026 Feature
AI Generated·May contain errors
Video Summary
AI Generated
This video introduces `Math.sumPrecise()`, a new static method arriving in ECMAScript 2026. It solves the long-standing issue of floating-point precision errors in JavaScript, where adding decimals (like 0.1 ten times) often results in imprecise values. The method ensures mathematical accuracy by avoiding intermediate rounding errors, making it ideal for financial calculations and e-commerce totals.
Gais, tell me this: if I give you ten paise to you
Ten coins, two of them—how many paise did I give you in total?
Think, obviously I gave one rupee, right, because ten paise
If I give you ten, it will be called one rupee, right?
But in the world of JavaScript, these people have been interested for the past 30 years.
Check how I'll tell you even for this charge.
I'm just walking here, okay? I haven't done anything.
I put ten paise inside the simple box ten times.
It's kept here; I just need to figure out its sum, and two basically
Find some, we use the reduction method, we all
You know, it's like evergreen, isn't it? Not for loop, so very little
It must have been done, but now if I run it, you'll see
They ate up a penny's worth of interest.
I'm doing something about it.
I need a small part too
Look here, right here
So I'd like to tell you, guys, finally today we're going to see a method like this
Has finally put an end to this, which had been going on for the past 30 years.
Yes, guys, and how did it happen? Let me tell you, it happened with the help of
of one new static method that has just recently come onto the baseline in 2026
You will see it in ECMAScript 2026 and
The name of the stating method is Gais, you
Here you can see, and all right or this is it
Alright, so here you can see its name is this
Yeah, I control it as S-EP, and you
Finally, a precise method
which is inside your math object
You get the built-in one.
If you use it, then finally
After 30 years, you can see it now
that exactly one is visible to you
So this in itself
It's a big deal, guys
Finally, you were able to see that result.
Whom we had been waiting for since then
No doubt, by rounding it up and doing all those things, you can easily get one.
It would be good, wouldn't it, if we got it without having to do anything?
And why would something have gone wrong in 2026?
Before moving forward in the video, I'd like to mention Gais
If you want to study with me in live online classes
So, from today, our backend development is going to start.
Today we are starting node and express at exactly 8 p.m.
So if you're interested, guys, you can enroll in this course.
Only and only 1999 rupees? That's very little money.
So, guys, today's main hero of our video is this one.
That's what Math.dot sum
Precise, all right, let me show you.
What exactly is this? Look here, and
How exactly is it giving you the correct answer?
The back, look here
You can see it very clearly
It is a static method.
takes an iterable of numbers
and returns their sum
Normal, we've already seen the example.
but how can it give you the exact correct precise amount?
It's giving him because it avoids
Look here, because it avoids it
floating point precision loss that occurs
It makes him avoid guys like that.
Basically, it also adds this sum to him.
That's all.
Just watch the animation I'm showing you.
So how is this math.sum precise method able to give the result exactly correct?
So the simple reason is this
Whenever we add numbers one by one
So, the small rounding error at each step accumulates.
It adds everything together precisely, just like exact mathematics.
And only at the very end does it round once.
I hope you understood that we add everything together and finally round it off.
Therefore, it is able to give you the correct answer.
And here's another example, look at this one too — I'm copying it, okay, this one
It's a very large number; if you place a small number in between, wouldn't it be?
So what's the problem, I'll show it to you here, just one second
And I'll just place it here like this, okay?
Let's say I'll just take a quick look and then put it this way.
Now look carefully, I have now changed the array.
We have changed the value inside it.
Two very large numbers, all the children are numbers.
He basically just ignored it.
Your reduce method is also wrong here.
There's also something wrong here.
It's correct here too.
And here is also correct.
The expected output is 0.1.
How did this come about if you want to know
Hey, bro, what are these numbers?
How can we add on minus this?
Please let me know in the comment section
Like, write anything you want, I don't understand.
Write E20, write E20, isn't the scene about ethanol happening right now?
If it's a fake like, just write E20, and I'll understand that you're using this method.
We need to see how we can do addition, but this
So, you'll say what is the use case for this?
There are many places where this use case can be applied, aren't there?
Look, whenever the matter comes up
It's fine to add up.
any iterable number, whenever the topic comes up
As for adding up, there is
Finally, if you want it absolutely precise
I need the exact correct number
Then use this, the best of it
The biggest example is when you create an e-commerce application.
You're finally adding to cart and liking the page, right?
Do
There, you need to calculate the subtotal first, then the total; what is that?
the single digit value of multiple numbers, not a single digit
but a single value, right, so that's where this will come in handy for you, for example
Let me show you, wait a second here, so you can see that add
are
The one for the cart, I've already added it here, basically you
Here, you can already see its output, no problem will occur.
But I'll zoom in a little so you all can see it clearly.
And I'm hitting this—check it out, guys
There is a difference in the output; one is that your value is 60.599994,
And here you are getting 60.6,
So now it's entirely up to you what to use here.
Right, I'm just showing you a use case.
where you are coming to in this manner,
There, when you use some precise method, you
Now, the second reduce method is the one we use the most.
When it comes to finding the average of something, right
So if I calculate its average, let's say just this one, that's fine
I'll copy both of them here; that's fine.
Now, I'm removing these two parts.
I'm liking these as console, and the array we had earlier
I'm placing this coin here; look, if you now average it
If we need to remove it, no doubt, here we'll just do a simple forward slash
and marks dot length, and I'll do the same for this as well
Okay, let's move on. Alright, I need to calculate the average ahead.
So marks.length, marks.length, and by the way,
It's not a mark, let's say it's just a coin, okay.
Just keep these coins, it'll work, okay? Now look here,
The average should be 0.1, right? It should be 0.1.
Right, we know this should come, but what actually comes?
Just look at that, and this is it—check this out, guys. Now, which one is this?
It's giving you this, right there, the precise sum of this one
If it's giving you an average, which one felt better to you?
You tell me, right? Now you must be feeling like, dude,
I mean, sum precise is dangerous—it's a killer for the reduce method, that's for sure.
Alright, it might be possible somewhere or other.
Without a doubt, both will either be equal or will win in most places.
Alright, this is your evergreen, we all know it, so act.
My script came out in 2009, perhaps this method
It'll be even better when it finally arrives in 2026, right?
Now there is a place where neither of these two things works.
These facts give you the reduced data output.
but does not give precise information,
Let me tell you such an example.
so let's say,
Now I have an array,
Right.
Let's say I have one array
where we have number and string together
which also has a string in my possession
number is also there
So now tell me what could possibly be here
By the way, this average here now
I don't want to stay here.
Okay, I'm not keeping average here.
Here, tell me one thing honestly—what do you think?
What could happen here is fine, I walk here.
And this is it, see?
What you're seeing here is 0, 1, 0, 2, 0
So basically, the reduce method has given you the output.
He basically did string plus number
Gave it as a concatenated string
right
But there, when you are using sum precise
Bro, he gave an error.
Saying this
Hey, how can you add a string with a number?
That is not possible.
That's why he said, just look here, what's written.
The value is not a number
So now guys, this happens in many places like this
It should be only a number, brother.
So it will be useful there too, and we'll keep winning the same way.
Alright, now look, every single thing should be exact, absolutely correct, even this one.
No, yes, but whenever the topic comes up regarding Sam, then this is always the case.
It will be correct, but when you're calculating the average,
So, at some point or another, there's also a little bit of problem in this.
It could be, because it will equalize everything.
coins.length
and obviously here too
I'll do coins.length, that's fine
I've done it here this way
Now watch, I'm leaving.
Seeing it finally like this
It's an example where both of them have come together at the same place.
No, whatever we have been seeing so far
So you must have thought it was precise, like it's god.
It's mode, right? Like always, precise, brother, correct, exactly.
I'm rounding it off perfectly for everyone, but the thing is
It usually rounds off right at the end, but in this case, it ended up failing.
because when he looked into the sum, there was no problem at all
but when he did divide, after that he did not round it off,
Because its job was only up to sum, wasn't it?
Whatever the output may be, let's say 50.
Then it got divided, so the work on sum precise here was already underway anyway.
Whatever it is, it's fine. So, look here, if you do it this way, just stay there, okay?
If you fix it, this problem of yours will be resolved—just look.
You're all right, aren't you? You don't have any problem, do you?
There's nothing you can do, not even reduce it like that.
But today, the hero of this video is this one, so let me just focus on this one.
I'll keep it a priority, you know, right? Or guys, this is it.
That's all for today's video. This has been your ECMAScript 20.
Look, people have gotten scared due to AI; they've forgotten that JavaScript
Every year in the annual release, some new updates come in.
You must definitely check all this, buddy. I've already seen it made by AI, but you need to stay up to date.
What will be introduced in ECMAScript 2026? Which new methods will arrive?
New methods or properties have been introduced which are evergreen.
It's a test, so nothing has been deprecated—still, you must definitely check everything, guys.
And I'm telling you again, starting today, the Node.js we have
The live online class is about to start, so if you are
If you want to learn live with me, you can join.
Please like the video and share it widely with your friends
If you also have any new property methods
The video I should make about what you want
So please write in the comments and okay, and right away
If you haven't subscribed to the channel yet, please do so now.
And the next target is 8 lakh subscribers
See you in the next video
Until then, guys, thank you so much
Take care.