About Monkey 2 › Forums › Monkey 2 Programming Help › types must be intergral ??
This topic contains 9 replies, has 5 voices, and was last updated by 
 codifies
 2 years, 2 months ago.
- 
		AuthorPosts
 - 
		
			
				
January 20, 2017 at 1:21 pm #6664
I get an error trying to do this:
[/crayon]Monkey123[crayon-5cba8438aca1d565787479 inline="true" ]local X:int = Floor(v) & 255yet I can (but would rather not!) do this:
[/crayon]Monkey1234[crayon-5cba8438aca23565826481 inline="true" ]Local t:Int = Floor(v)local X:int = t & 255is this a bug? or am I missing something – don’t want to waste Marks time reporting a bug if its just my misunderstanding…
January 20, 2017 at 3:55 pm #6665Floor is returning a double! So for me it’s kind of buggy.. Should return int or long.
The second example works because there’s an implicit convertion before calling the & operator?January 20, 2017 at 6:34 pm #6667this also fails
[/crayon]Monkey123[crayon-5cba8438b362d434877291 inline="true" ]local X:int = Int(Floor(v) & 255)this however works….
[/crayon]Monkey123[crayon-5cba8438b3633614375519 inline="true" ]local X:int = Int(Floor(v)) & 255January 20, 2017 at 7:10 pm #6668the first fails as your also wrapping the &255 in the brackets, this is why the second one works.
This could be a throwback to C casting where each section had to have a cast or it could be down to the function returning something other than a int.
January 20, 2017 at 7:54 pm #6669could you explain why? so what if the & 255 is within the brackets it should all be evaluated to a single value cast to an int and put in the variable surely ???
January 20, 2017 at 9:21 pm #6671writing
[/crayon]Monkey123[crayon-5cba8438bb6f6195779980 inline="true" ]local X:int = Floor(v) & 255orlocal X:int = Int(Floor(v) & 255)is I think exactly the same..
It will first compute “Floor(v) & 255” then convert it to an int if it is not an int.The problem here is that there is not implicit conversion with the & operator and that Floor is returning a double but should return an int or long (integral).
if you:
[/crayon]Monkey123[crayon-5cba8438bb6fd133203898 inline="true" ]print 3.0 & 255you’ll have the same problem, one of the types is not integral
if you:
[/crayon]Monkey123[crayon-5cba8438bb702443965844 inline="true" ]print int(3.0) & 255it’ll first make an explicit conversion from 3.0 to 3 then “&” is in a “int & int” situation therefore have integral types hence is happy.
IMHO this can be a kind of bug as Floor is made to transform a real variable to an integral one. So it should return an integral type not a double(wich is a real).
Ceil has the same problem..January 20, 2017 at 9:58 pm #6673IMHO this can be a kind of bug as Floor is made to transform a real variable to an integral one. So it should return an integral type not a double(wich is a real).
Ceil has the same problem..oops no
http://stackoverflow.com/questions/15348180/why-doesnt-floor-return-an-integer
so you have to explicitly convert to an integer by yourself… and don’t play with numbers larger than MAX_LONG/MAX_INT or NaN or Infs! Or check for it before trying the conversion.
January 21, 2017 at 12:33 am #6676Based on the rules I know from blitzBasic and BlitzMax which have helped me with Monkey2 so far the rule is:
doubles take precedence over float and floats take precedence over int. So in a direct formula with with doubles,
floats and integers(no brackets included and no casting) everything will be cast to double and the result will be double no matter the order. if the formula includes floats and integers everything will be cast to float and the result will be a float. same principle with double and int. I am not an expert on the subject but it has helped me well until now.January 21, 2017 at 3:16 am #6678Floor is returning a double!
Yes it does! It’s a frequently used op in floating point math too…
So you need to cast to int in this case, eg: Int( Floor( blah ) ) & 255, because you can only use the ‘&’ operator with integral types.
local X:int = Int(Floor(v) & 255)
This fails because compiler evaluates what’s inside () first, which in this case is Floor(v) & 255, which is the original problem expression.
January 21, 2017 at 11:34 am #6696right, got it! the point being & will only work with integrals
its a pity the error message doesn’t include an indication how far along the line the error is, Java does this and its quite handy!
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.