2

I have an expression which I am evaluating both in Java and JavaScript.

Java

System.out.println(582344008L * 719476260);

Output: 418982688909250080

Javascript

document.write(582344008 * 719476260)

Output: 418982688909250050

Why is there a difference in both values?

Michael
  • 41,989
  • 11
  • 82
  • 128
Ankit Arora
  • 110
  • 3
  • 16

2 Answers2

2

Javascript numbers are all IEEE 754 doubles, so it looks like there is some floating point error going on here. see Difference between floats and ints in Javascript? for some more details

  • That makes sense. The result of `582344008.0 * 719476260.0` in Java is indeed `4.1898268890925005E17`, which is the same as the result reported for JavaScript. – Ole V.V. Mar 05 '19 at 18:16
2

Preamble

As @Charlie Wallace says

582344008 * 719476260 is greater than Number.MAX_SAFE_INTEGER

What is JavaScript's highest integer value that a number can go to without losing precision?
As **@baseballlover723
and @RealSkeptic say it's a floating point error in precision/different storage type size rounding error.

See javascript types:

What are the 7 primitive data types in JavaScript

There are 7 basic types in JavaScript.

  • number for numbers of any kind: integer or floating-point. Integer/Decimal values up to 16 digits of precision. JavaScript numbers are all floating point, stored according to the IEEE 754 standard. That standard has several formats. JavaScript uses binary64 or double precision. As the former name indicates, numbers are stored in a binary format, in 64 bits.
  • string for strings. A string may have one or more characters, there’s no separate single-character type.
  • boolean for true/false.
  • null for unknown values – a standalone type that has a single value null.
  • undefined for unassigned values – a standalone type that has a single value undefined.
  • object for more complex data structures.
  • symbol for unique identifiers.

The typeof operator allows us to see which type is stored in a variable.

  • Two forms: typeof x or typeof(x).
  • Returns a string with the name of the type, like "string".
  • For null returns "object" – this is an error in the language, it’s not actually an object.
  • What are the 8 primitive data types in Java?

        byte     for  8-bit signed integer  
        short    for 16-bit signed integer  
        int      for 32-bit signed integer  
        long     for 64-bit signed integer  
        char     for two bytes in Unicode  
        float    for *decimal* values up to  7 digits of precision   
        double   for *decimal* values up to 16 digits of precision (64 bits)  
        boolean  for true/false  
    

    Decimal values with a fractional component is called floating point. They can be expressed in either standard or scientific notation.

    Testing

    A quick way to test is use an online java compiler (Compile and Execute Java Online (JDK 1.8.0))

    public class HelloWorld{
    
         public static void main(String []args){
            System.out.println("Long Output: "+582344008L * 719476260L);
            System.out.println("Float Output: "+582344008.0 * 719476260.0);
         }
    }
    

    The command:

    $javac HelloWorld.java
    $java -Xmx128M -Xms16M HelloWorld
    

    The Output:

    Long Output: 418982688909250080  
    Float Output: 4.1898268890925005E17
    

    My desktop calculator goes to this:

    4.189826889E17
    

    and for JavaScript online compiler:

    //JavaScript-C24.2.0 (SpiderMonkey)
    print("Output: "+582344008 * 719476260)
    

    The Output:

    Output: 418982688909250050
    
    Jon Goodwin
    • 9,053
    • 5
    • 35
    • 54