Results 1 to 8 of 8
  1. #1
    Iron Guard
    Join Date
    Sep 2008
    Location
    Utah or California
    Posts
    566
    Gamer IDs

    Gamertag: Marscaleb PSN ID: Marscaleb

    Default Float result of int numbers [answered]

    I'm getting hosed by some calculations that are coming out wrong because the values are ints instead of floats.

    Let's say we've got
    Code:
    var int X, Y;
    X = 100;
    Y = 90;
    ...and I've got a check system set up
    Code:
    if ( X/Y < 1)
    `log("A");
    else if ( X/Y > 1 )
    `log("B");
    else if ( X/Y == 1)
    `log("C");
    Now for this example I could just check if (X > Y) but this is just an example. This keeps popping up for me as a problem in all sorts of places.

    If I run that code above, my result is going to be "C" because X/Y will NOT calculate out to 1.11111 but instead to 1.
    I thought I could typecast the calculation by doing
    Code:
    if (float(X/Y) < 1)
    but that calculates out to be 1.00000 because X/Y still calculates to an int value, which I just change into a float.
    I tried
    Code:
    if (float(X)/float(Y) < 1 )
    but that gives me a compile error saying there's a type mismatch.

    There are a couple things I am interested in knowing.

    1) Where and when are values changed between types? Like, if I have a line of code that starts with declaring "intVar = " how will that effect how things are calculated as ints vs floats? What about "intVar += "? I need to understand when values are changed between types so I know when to expect a problem. There have been times when I have anticipated a problem and didn't get one, and vice-versa.

    2) What is the proper way to calculate int values to float results? Like say I'm feeding a variable the result of a long-A calculation, and all the variables inside are ints, but I want the calculations done as floats so that I don't lose precision halfway through? Like if I have
    Code:
    Local int X, Y, Z;
    
    Y = 1024;
    Z = 1463;
    X = Y * (2048 / Z);
    I would get X = 1024 when I'm expecting to get X = 1433.

    I could copy these out into float duplicates like
    Code:
    Local int X, Y, Z;
    Local float fX, fY, fZ;
    
    Y = 1024;
    Z = 1463;
    fY = Y;
    fZ = Z;
    fX = fY * (2048 / fZ);
    X = fX;
    which is what I have been doing, but this takes up more time and memory, both for programmer and computer. I'm sure there's a smarter way using typecasting, but I don't know all the rules for that, nor for the equations as they stand, to know how to use that efficiently.
    Last edited by marscaleb; 07-11-2012 at 04:48 PM.
    Follow my progress at:
    http://marscaleb.com/
    Read my new webcomic: Mischief in Maytia
    http://maytiacomic.com/

  2. #2
    God King
    Join Date
    Jan 2010
    Location
    Germany
    Posts
    4,133

    Default

    Code:
    Local int X, Y, Z;
    
    Y = 1024;
    Z = 1463;
    X = Y * (2048.f / Z);
    Things are automatically typecasted to the "most precise" data type that is part of the operation. So if you divide 2048.f by your Z integer, the result will be a float. And thus the next operation with the Y involves a float again and the result will be a float as well.

    For your initial problem could you try some hacky way like
    Code:
    if ((X * 1.f)/Y < 1.f)
    Our Loop, which art in source code, hallowed be thy keyword.
    Thy condition come, thy instruction be done, in RAM as it is in cache.
    Increment us this day our daily counter,
    and forgive us our typos, as we also have forgiven our compilers.
    And lead us not to the nullpointer but deliver us from bugs.
    For thine is the API, the GUI, and the CLI while(true).
    Semicolon;
    Please don't send me questions about how to do something in the UDK via PM. That is better discussed in the forums and we only have limited PM storage.

  3. #3
    Boomshot
    Join Date
    Aug 2011
    Posts
    2,920

    Default

    Code:
    if (float(X)/float(Y) < 1 )
    I would try that again because it's perfectly valid. You don't need to cast the second operand to float either.

    Code:
    if( float(x) / y < 1 )

  4. #4
    Iron Guard
    Join Date
    Sep 2008
    Location
    Utah or California
    Posts
    566
    Gamer IDs

    Gamertag: Marscaleb PSN ID: Marscaleb

    Default

    Quote Originally Posted by Crusha K. Rool View Post
    Things are automatically typecasted to the "most precise" data type that is part of the operation. So if you divide 2048.f by your Z integer, the result will be a float. And thus the next operation with the Y involves a float again and the result will be a float as well.
    The most precise data type... does it check the whole equation for what that data type will be, or does it move everything over when it finds that data type?

    So if I had some long sequence with a float near the end, do I not receive that float precision until the equation reaches the float, or does it work in floats right from the beginning?

    And what if I was dealing with a bunch of int numbers but the final result was being saved into a float variable? Does it run the calculations as ints and then give that value to the float, or does it calculate everything as a float, knowing that's what it is going into?

    Quote Originally Posted by Spoof View Post

    I would try that again because it's perfectly valid. You don't need to cast the second operand to float either.
    That's what I thought but it gave me a compile error. Though, I just went back and tried it again and it worked, so I guess I had mistyped something but I don't know what. Oh well. X < Y is a better way to run it anyway.
    Follow my progress at:
    http://marscaleb.com/
    Read my new webcomic: Mischief in Maytia
    http://maytiacomic.com/

  5. #5
    God King
    Join Date
    Jan 2010
    Location
    Germany
    Posts
    4,133

    Default

    Quote Originally Posted by marscaleb View Post
    The most precise data type... does it check the whole equation for what that data type will be, or does it move everything over when it finds that data type?

    So if I had some long sequence with a float near the end, do I not receive that float precision until the equation reaches the float, or does it work in floats right from the beginning?
    It follows the mathematical order, so any operation that uses int on both sides will result in an int value until a float is eventually involved.
    Our Loop, which art in source code, hallowed be thy keyword.
    Thy condition come, thy instruction be done, in RAM as it is in cache.
    Increment us this day our daily counter,
    and forgive us our typos, as we also have forgiven our compilers.
    And lead us not to the nullpointer but deliver us from bugs.
    For thine is the API, the GUI, and the CLI while(true).
    Semicolon;
    Please don't send me questions about how to do something in the UDK via PM. That is better discussed in the forums and we only have limited PM storage.

  6. #6
    Boomshot
    Join Date
    Aug 2011
    Posts
    2,920

    Default

    Evaluation order and precedence are important considerations.

    Code:
    local int i1, i2;
    local float f1, f2, result;
    
    i1 = 5;
    i2 = 6;
    f1 = 3.6;
    f2 = 6.3;
    
    result = i1 / i2 + f1 / f2; // 0.5714
    
    result = float(i1) / i2 + f1 / f2; // 1.4048
    The first expression evaluates as:
    Code:
        (i1 / i2) = 0
        (f1 / f2) = 0.5714
        (0 + 0.5714) = 0.5714
    The second expression casts i1 to float, which resolves the first group to float.
    Code:
        (float(i1) / i2) = 0.8333
        (f1 / f2) = 0.5714
        (0.8333 + 0.5714) = 1.4048
    And the compiler interprets literal numbers as integer unless you provide a decimal point or the 'f' suffix.

    Code:
    `log( 1 / 2 );    // 0 (int)
    `log( 1.0 / 2 );  // 0.5 (float)
    `log( 1.f / 2 );  // 0.5 (float)
    `log( 1.0f / 2 ); // 0.5 (float)

  7. #7
    Veteran
    Join Date
    Sep 2006
    Location
    Unreal Nomad
    Posts
    7,682
    Gamer IDs

    Gamertag: ambershee

    Default

    As a general practice, if I have any kind of arithmatic that involves integers and a division / multiplication with floats, I'll cast the int to a float. The reason for this is that other people have a tendency to miss the integer when working with your code later down the line, and introduce rounding errors.
    - Please do not send me questions regarding programming or implementing things in UDK via Private Message. I do not have time to respond and they are much better answered in the forums. -

  8. #8
    Iron Guard
    Join Date
    Sep 2008
    Location
    Utah or California
    Posts
    566
    Gamer IDs

    Gamertag: Marscaleb PSN ID: Marscaleb

    Default

    So it never casts ints into floats until it encounters a float in its mathematical sequence, so I could cast my ints into floats when I divide.

    Okay! Thank you all for the explanations and help!
    Follow my progress at:
    http://marscaleb.com/
    Read my new webcomic: Mischief in Maytia
    http://maytiacomic.com/


 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Copyright ©2009-2011 Epic Games, Inc. All Rights Reserved.
Digital Point modules: Sphinx-based search vBulletin skin by CompletevB.com.