Working with Long in Processing

edited March 2017 in Questions about Code

Consider the following code: int radiusCubeInt = 10000 * 10000 * 10000; println(radiusCubeInt);

long radiusCubeLong = 10000 * 10000 * 10000;
println(radiusCubeLong);

I am getting -727379968 in both cases. It seems like Processing does not implicitly treat 10000 as long, does it meant I have to type convert every number in a for a statement evaluating to a long?

Tagged:

Answers

  • Does using 10000L work?

  • Answer ✓

    It's got nothing to do w/ Processing, but Java itself! ~O)
    At least 1 of the operands gotta be of datatype long in order to trigger auto coercion for the others.

    We can suffix a literal w/ L in order to indicate it is long instead of int: *-:)
    long radiusCubeLong = 10000L * 10000 * 10000;

    For better performance & assurance, prefix every1 w/ L as well: >-)
    long radiusCubeLong = 10000L * 10000L * 10000L;

  • Out of curiosity, what can I add F to convert integer to float when I do integer division?

  • edited March 2017 Answer ✓

    5.0/2 will work with floats, 5/2 will work with integers. Same with 5/2.0. And I believe 5/2f should also work (this is obviously for Processing only, not plain Java) - confirmed by @quark.

    @GoToLoop Something I find strange in Java is that if you use something like 5.0 it is considered a double (= 8 bytes), but 5 is considered an int (= 4 bytes). Why was this done?

  • edited March 2017 Answer ✓

    Yes for example

    println(22/7);    // 3
    println(22f/7);   // 3.142857
    
  • Another question

    int A = 2;
    int B = 3;
    float C = float(A)/float(B);
    println(C); // 0.6666667
    

    I can use float() to convert A from int to float. How do I convert a float to double then?

    float A = 2;
    float B = 3;
    double C = double(A)/double(B);
    println(C); //Error: The function parseDouble(float) does not exists.
    
  • I think you may not need to. I do not remember. But this will work -

    double C = A/(double)B;

  • It does work. So what is the difference between, say float(A) as compared to (float)A, I believe the former is type conversion and the latter is type casting.

    From my understanding (I can be very wrong), type cast reads the bits on memory and interpret it another type. Like 00000111 01011011 11001101 00010101 in memory can originally be the integer 123456789, but casting it to float will interpreted it as 2.16249752287100808705679454548E-38 instead. Casting it to boolean will probably make it true.

    Unless that is my intention (what practical application would that be?), why wouldn't I just use type conversion all the way? And why is there no type conversion for double, but there is one for float and int?

  • edited March 2017

    Now you confused me. Just read about "explicit casting in Java" for more info.

  • When Processing was first created they made the decision that they would use the float data type throughout and ignore the double data type.

    Personally I think this was an unfortunate choice since most external libraries / code we might want to use take the reverse view and favour the double data type.

    The methods float(A) and int(A) are methods added by Processing. They don't provide a method called double(A) see above.

    (float) A
    (double) A
    (int) A
    (long) A
    

    are simple Java type casts.

  • Answer ✓

    wouldn't I just use type conversion all the way?

    Type casting between numerical data types is much more efficient than using the Processing methods.

  • But why did Java choose to use double? It would've made sense if long was the preferred data type for integer (in mathematical sense) values.

  • edited March 2017

    I guess double was picked as fractional datatype default to please scientists and other data crunchers.

    Imagine a scientist trying to write a program and wondering why Java's numerical fractional's precision is much less than any pocket calculator! =))

    They'd rather probably give up on Java than finding out by chance they'd need to suffix literals w/ d for 64-bit floating-point precision! 8-X

    For fixing-point integer's case, int is pretty much all we need for most cases, considering MAX_INT is more than 2 billions already! \m/

  • I see. But Processing wasn't meant for such guys. It's for graphical design. Hence a different choice. \m/

  • IMO, Processing using float as default was a mistake. It doesn't actually make more sense for graphical design at all.

  • edited March 2017

    Because the accuracy issues can still be a problem, and the performance difference can be negligible. Of course, for interacting at a low level with things like OpenGL then floats are useful, but for vast majority of Processing usage, double would probably be a better option.

    EDIT - to be fair, at the time Processing was first introduced the above may not have applied.

  • Casting doubles to ints is possible?

  • Yes ... if that's relevant? :-/

  • In fact, right now I'm a bit confused, once I get the fundamentals right in my mind, I'll come back to this discussion. Thanks anyway.

Sign In or Register to comment.