Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Sorry for this huge lack of knowledge, but, Can someone explain me what is happening? Why does it says 511 instead of 777?
-
firusvg13767y@mikessoldier Leading 0 denotes that number is in octal (thus base 8) representation. ECMA 5th edition changed this (so leading zero doesn't denote octal but "regular" integer in base 10). /cc @ukabthebest
-
@mikessoldier He is using the octal system (base 8) by adding a 0 to the front. Octal only accepts numbers 0 to 7(afaik), so 777 is correctly put out as 511, but 888 is not an actual octal number, so javascript does its thing and seems to read it as decimal (base 10).
@firusvg damn, i was too slow :P -
Adding a 0 in front turns it into an octal. 777 in base 8 is 511. 888 is an invalid base 8 number, therefore the interpreter cuts off the first 0 and keeps it in base 10.
-
@mikessoldier 0*** is Octal Notation. So every number that starts with a zero will be tried to be interpreted as an Octal number. Well except 0x***, which is hexadecimal.
Octal notation uses digits from 0 to 7. When you use 8, 9, it is no longer a valid octal number. Javascript thus sees it as a decimal number. -
Thanks for explaining everyone!
Now the question is... Does anyone know why this exists within Javascript? Not sure about you, but I don't convert things to octal... Ever. -
mt3o19137y@tylerleonhardt you don't, but this doesn't mean no one else doesn't. Octal base is useful as a part of byte. There are use cases you need to operate on bitmasks, having a type for this is handy.
-
@abchin that was using the Node.js REPL that's built in to Node.
The terminal was in iterm on a Mac but you can get to this REPL by installing Node and typing node in your terminal of choice. -
abchin47y@tylerleonhardt thanks a lot, your mention of the REPL helps me a lot. I would not struggle to debug front end or back end code any longer.
-
@abchin between that and the console in your browser of choice are the best options for interacting with js quickly and simply
-
@orijin that's a common thing for most, if not all programming languages. The thing is that floating point types are always approximations of the real value. In some cases this results in such situations. They are easy to spot. Just add numbers like *.3, *.2, *.7, to get to numbers like *.9 or *.7. You'll find loads of them.
Because of this, you should never compare floating point numbers with the equality operator, and always compute the absolute value of the difference and test it against a threshold.
For example, Math.abs(a-b)<0.00001 -
@orijin P.S.
That's why when dealing with situations that require high precision, platforms like Java or .NET use fixed point numbers like System.Decimal in .NET. They don't suffer from precision loss. In RDBMS they are presented as DECIMAL(n, p) or NUMERIC(n, p), where n is the total size of the number (how many digits plus the dot), and p is the precision (number of decimals). For example 162.34 would fit in NUMERIC(6,2).
So what about PHP and JavaScript, and maybe other platforms?
Well, a working approach is to work with Integers instead.
Option 1. When working with money, store and calculate everything not in dollars, but in cents. So price is not 6.99 but 699. An integer. Never lose precision again. When displaying, divide by 100. That's it.
Option 2. Make your own type or find a library that has this, which store 2 integers, one for the integer part, and the other for the decimals. Implement arithmetic operations, and you're set.
Related Rants
🙃 Javascript is beautiful 🙃
undefined
node
nodejs
wat
octal
js
javascript