Whats javascript floating points

Tell us what’s happening:

I have been completing this challenge, i completed it but I’m a bit curious about this floating points that is said as a note. I tried searching on the web but i can’t understand it. Can you teach me in a way i can understand?

Also I am doubtful about the previous challenge with ++ operator. I searched for it on the web and that made me very confused.

This is the link to what i found on web. I have a few Questions on it:

  1. On the first try it example, after running it why did it say x:4, y:3. The x was declared as 3 and only y was declared as x++. The results should have been x:3, y:4. Am I right?

  2. What is meant by returning the value before incrementing and after incrementing? What is return in javascript?

These are the Questions i have right now.

Thank you for reading this topic :heart:

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0

Challenge Information:

Basic JavaScript - Create Decimal Numbers with JavaScript

1 Like

It always helps if you include a link to the challenge so we know exactly what you are referring to and don’t have to spend time trying to figure it out. I believe you are referring to the challenge Create Decimal Numbers with JavaScript.

As that challenge says, floating point numbers are numbers with decimals in them. Because there are an infinite amount of these numbers, computers have to approximate their values when they store them in memory. Thus, calculations using floating point numbers won’t always come out the way you think they will because they can’t be as precise as you might like them to be due to these approximations. For example, open the console in your browser tools and type the following equation:

4.001 - 3.001 

You and I both know that the answer is 1. What does JS think it is?

Granted, the answer JS gives is pretty dang close to 1, but not perfect. Depending on the values you are working with these irregularities could affect your calculations. That’s what the note was trying to explain to you.

1 Like

Hello @madhavvattackattu !

To add to @bbsmooth excellent guidance, I thought I would provide a link to a tutorial that may help you understand the floating points. It explains the floating points.

1 Like

I extremely sorry about the link to the project, i had already included the link but it was pushed down by the text i added above.

I understand floating points now. You explained it in a simple way. I calculated your equation on the console and i was expecting it to give me 1 as the answer, but as you said it gave me the answer 1.0000000000000004 .

Thank you for explaining this to me :heart:

3 Likes

Hi @Gray.n.Grey

Yes @bbsmooth gave me an excellent guidance. The Link you provided me explains me more about floating points with interactive try it problems.

Thanks a lot :heart:

2 Likes

You are very welcome @madhavvattackattu !

That site has good tutorials that I sometimes use to complement my learning on here.

Happy coding!

2 Likes