News for hackers. Stuff that matters.
Re:Be careful with JS numbers by meaty on Sunday January 13, 2013 @03:06PM (#5051710)
This is one reason I stick to statically typed languages. I do a lot of work with cash values and without an explicit decimal type, the shit hits the fan when you hit a float issue.

I get a lot of flak on here for that opinion which is odd.

Re:Be careful with JS numbers by bphogan on Sunday January 13, 2013 @03:28PM (#5051759)
Dynamically typed languages are not the same as weak typed languages. Dynamically-typed languages, like Ruby, still have data types like String, Float, etc. I've seen people use floats in statically typed languages before and end up with rounding errors simply because they chose Float instead of Decimal.
Re:Be careful with JS numbers by meaty on Sunday January 13, 2013 @04:48PM (#5051992)
I am fully aware of the differences between weak typing and dynamic languages.

The issue I have with dynamic languages with types is that the type is usually inferred based on the operation. It's very easy to make mistakes and for those mistakes not to be apparent until the shit hits the fan.

Fully statically typed languages put these concerns right in your face and make you think about them.

Re:Be careful with JS numbers by chimeracoder on Sunday January 13, 2013 @06:32PM (#5052361)
> Fully statically typed languages put these concerns right in your face and make you think about them.

That depends slightly - if your compiler uses Damas-Milner type inference, it's possible that the compiler will assign the most general type signature possible to your function, which will not prevent this type of error, as operations such as addition work on either integral or floating point numbers.

What you're really after is a strongly typed language that also has explicit type declarations (or a programmer that's reliable enough always to include them even when they are optional).

Re:Be careful with JS numbers by artsrc on Monday January 14, 2013 @01:04AM (#5053476)
One counter example is Java and Python. In Java this is allowed and the result requires knowledge of detailed rules:

    int i = 10;
    int j = 20;
    String s = "Test " + i + j;
In Python the equivalent is an error.

The 'in your face' difference between a statically typed language and dynamic one is the time difference between the compilation error and an execution error. One tool for working effectively with dynamically typed languages is to keep that time difference short with tools like unit tests.

Re:Be careful with JS numbers by masklinn on Sunday January 13, 2013 @06:54PM (#5052441)
> The issue I have with dynamic languages with types is that the type is usually inferred based on the operation.

That's about as wrong as can be.

In fact, it's the exact opposite (type inference can only be a property of statically typed languages)

Re:Be careful with JS numbers by jre on Sunday January 13, 2013 @03:13PM (#5051727)
It's not really about static typing or not, it's really about JS having some really strange and counter-intuitive behaviours in a lot of cases (numbers, default method scope, truthy values, ...). Part of it is probably due to the fact that the syntax looks so much like C and Java that you'd except it to behave the same. Part of it seem to be that it's hard to fix things without breaking existing codebases.
Re:Be careful with JS numbers by ricardobeat on Sunday January 13, 2013 @03:40PM (#5051788)
Every language has it's own quirks, some even weirder than JS. The floating point "errors" are common to a hundred programming languages, that's why BigNum classes/extensions exist.
Re:Be careful with JS numbers by jre on Sunday January 13, 2013 @04:12PM (#5051872)
The problem in this case is not floating point approximations. It is that some functionalities (parseFloat vs parseInt, ++, 45.0 being printed 45 in most browsers) makes the user believe that JS has an integer type.
Re:Be careful with JS numbers by ricardobeat on Sunday January 13, 2013 @05:16PM (#5052111)
Square peg in a round hole, you wouldn't try to understand Haskell with a Ruby background either.

Since it has no types per se, it doesn't make much sense to think of an 'integer type'. Javascript only has a number primitive, which is a IEEE 754 float.

Re:Be careful with JS numbers by Avshalom on Sunday January 13, 2013 @04:56PM (#5052032)
This is about JS having a non existent numerical stack. A statically typed language which only had floats would have the same issues.
Re:Be careful with JS numbers by mmahemoff on Sunday January 13, 2013 @03:10PM (#5051718)
Dynamically typed languages still have types. Such as BigNum.
Re:Be careful with JS numbers by meaty on Sunday January 13, 2013 @04:50PM (#5052007)
Yes and they are cumbersome.
Re:Be careful with JS numbers by khyryk on Sunday January 13, 2013 @03:34PM (#5051767)
Python has a decimal module, so this is not exclusive to statically typed languages.

http://docs.python.org/2/library/decimal.html

Re:Be careful with JS numbers by meaty on Sunday January 13, 2013 @04:50PM (#5052005)
Yes but conversion and over the wire, it becomes a pain in the arse as a language extension.
Re:Be careful with JS numbers by pserwylo on Sunday January 13, 2013 @04:07PM (#5051858)
Dynamically typed vs Weakly typed languages aside, you still only have one choice in the browser. Sure, you can write in something which has all the greatest features you love, but it will still be cross compiled to JavaScript, with all of its pitfalls right?
Re:Be careful with JS numbers by meaty on Sunday January 13, 2013 @04:45PM (#5051978)
Simple solution: I wouldn't write anything that requires numeric accuracy to run in the browser.
Re:Be careful with JS numbers by derleth on Sunday January 13, 2013 @08:19PM (#5052753)
> Sure, you can write in something which has all the greatest features you love, but it will still be cross compiled to JavaScript, with all of its pitfalls right?

By this logic, no bignum library could exist because machine code doesn't have bignums, and everything gets compiled to machine code and/or run by an interpreter that's been compiled to machine code.

Re:Be careful with JS numbers by shoopy on Sunday January 13, 2013 @10:12PM (#5053098)
For some reason, people here think Javascript is the best shit ever.