types must be intergral ??

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.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #6664

    codifies
    Participant

    I get an error trying to do this:

    [/crayon]

    yet I can (but would rather not!) do this:

    [/crayon]

    is this a bug? or am I missing something – don’t want to waste Marks time reporting a bug if its just my misunderstanding…

    #6665

    abakobo
    Participant

    Floor 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?

    #6667

    codifies
    Participant

    this also fails

    [/crayon]

    this however works….

    [/crayon]
    #6668

    EdzUp
    Participant

    the 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.

    #6669

    codifies
    Participant

    could 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 ???

    #6671

    abakobo
    Participant

    writing

    [/crayon]

    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]

    you’ll have the same problem, one of the types is not integral

    if you:

    [/crayon]

    it’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..

    #6673

    abakobo
    Participant

    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..

    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.

    #6676

    Jesse
    Participant

    Based 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.

    #6678

    Mark Sibly
    Keymaster

    Floor 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.

    #6696

    codifies
    Participant

    right, 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!

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.