We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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?
Answers
Does using
10000L
work?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 islong
instead ofint
: *-:)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?
5.0/2
will work with floats,5/2
will work with integers. Same with5/2.0
. And I believe5/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), but5
is considered an int (= 4 bytes). Why was this done?Yes for example
Another question
I can use float() to convert A from int to float. How do I convert a float to double then?
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 integer123456789
, but casting it to float will interpreted it as2.16249752287100808705679454548E-38
instead. Casting it to boolean will probably make ittrue
.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?
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 thedouble
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.are simple Java type casts.
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 iflong
was the preferred data type for integer (in mathematical sense) values.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-XFor 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.@neilcsmith_net Why do you say so?
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.