We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have been struggling with one of my variables reading NaN and 'infinity'. I have been struggling with it for 2 hours. If I copy and paste the formula for "radius" in my program into wolframalpha, I get a real result. Anyone have any ideas?
void setup() { size(800, 600); background(255); }
float px = 425; float py = 350; float radius=5*atan(5/(1000+800-425))/atan(5/(400+1000));
void star() { fill(#F6FF00); ellipse(400, 300,10,10); }
void planet() {
print(radius); radius = 5*atan(5/(1000+800-px))/atan(5/(400+1000)); ellipse(px,py,5,5); }
void draw() { star(); planet(); }
The variable 'radius' is the problem. It prints the variable as 'NaN' once and then 'Infinity' as it loops through the program.
I took out 90% of the program so it fits in the post. The calculation is superfluous for the written portion on this post but it is important for the rest of the program.
Answers
In Java the division operator
/
removes the fractional part from the result if both operands are of whole type. L-)https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
float radius=5*atan(5./(1000+800-425))/atan(5./(400+1000));
I apologizing for not formatting the text better.
I replaced the 5/(1000+... with 5./(1000+... in the program and it works great with no errors. Thank you for your time.
I am struggling why a programming language would drop the fractional/decimal part of a variable just because. Should I just put a decimal place after every integer I use to avoid this? And what do you mean whole type?
Thanks!
boolean
, the other 7 are numerical.float
&double
.char
,byte
,short
,int
,long
.As I've mentioned it's enough for at least 1 of the operands (the 2 expression sides of the operator) to be fractional. For example: for
1/2
we can1./2
,1/2.
or1./2.
. B-)Consistence & performance. Integer operations are faster than floating 1s after all.
If we declare that some variable is of type
int
and we try to assign4/3
to it, the result would need to be coerced fromfloat
ordouble
back toint
.Java automatically coerces upwards. But refrains for backwards conversions. 8-|
I would always add .0 rather than just a . because the latter just looks like punctuation and is easily overlooked