Why does it go black?

  • Videogamer555
    18th Jan 2012 Member 0 Permalink
    This C code is supposed to make the element go from black to almost white as you draw it from left to right. Yet it stays black no matter where it is. For some odd reason the "floor" function doesn't seem to be working, and keeps giving me 0 for the output. Floor is SUPPOSED TO return the integer part of a number but it's not working.

    parts[i].dcolour=0xFF000000+(floor(x/640*256)*0x010101);

    Never mind, it seems that if I add a .0 to the end of the numbers (for example 640.0 and 256.0) then it works. And I have no idea why.

    Even more confusing if I change the 640 to 640.0 and leave 256 as 256 it works. If I change 256 to 256.0 and leave 640 as 640 then it doesn't work. It seems that the computer is doing some kind of numerical conversions BEHIND THE SCENES without my knowledge!!!
  • jacksonmj
    18th Jan 2012 Developer 0 Permalink
    640 = int
    640.0f = float (floating point number)
    640.0 = double (like a float but uses more bits - greater precision)

    x is an integer, and 640 is an integer.
    Since both inputs to the division "x/640" are integers, integer division is performed - the result will be rounded towards zero to give an integer. Because x is less than 640 the answer is always 0.

    The computer switches from integer to floating point arithmetic the first time it encounters a floating point number in the expression. For x/640*256.0 the first float/double is after the integer division, so the damage has already been done. x/640*256.0 = 0*256.0 = 0.0

    For x/640.0*256, there is a floating point number in the division, which forces the division to be done using floating point arithmetic.

    You can also cast ("tell the computer to convert") x to a float or double to make it use floating point arithmetic for the division. floor(((double)x)/640*256)


    Search internet for "c integer division".

    P.S. please use correct section, this is not Lua scripting :-(
  • Videogamer555
    18th Jan 2012 Member 0 Permalink
    2 things, does the word "double" have to be in parenthethis for this?

    Also I just accidentally selected the wrong section, I meant it to go in the development help section. Why haven't the moderators moved it to the correct section?

    I like the way VB6 does this better. It always does floating point math, and only if writing the output of the equation to an integer variable (or sending it to the input of a function defined to have an integer input) does it round the number to an integer. And even the rounding is better. It doesn't always round down. It rounds to the nearest integer. So if something is 7.5 it becomes 8, and if it is 7.4999999 it becomes 7.
  • Pilihp64
    18th Jan 2012 Developer 0 Permalink
    @Videogamer555 (View Post)
    The way C division works is very standard and accepted, you can achieve the .5 round up/down mark by simply adding 0.5 to your floats before casting back to int, which is done quite a lot in the code already for element positions.
  • jacksonmj
    18th Jan 2012 Developer 0 Permalink
    @Videogamer555 (View Post)
    Yes, it has to be in parentheses.

    C does integer maths unless you specifically tell it to use floating point, because integer operations were historically a lot faster than floating point (the difference is smaller in modern CPUs).