Real or float : whats the difference?



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 26/02/2004 at 04:05, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   8.2 
    Platform:      
    Language(s) :     C++  ;

    ---------
    Hi
    I'm trying to determine a percentage value by using the following code

        
        
        
        
         LONG a = 10;  
         LONG b = 50;  
         Real rperc = a/b*100;  
         LONG lperc = a/b*100;
        
        
        
        
         float fa = 10;  
         float fb = 50;  
         float fperc = fa/fb*100;
        
        
        
        
         GePrint("Real: " + RealToString(rperc));  
         GePrint("Long: " + LongToString(lperc));  
         GePrint("Float: " + RealToString(fperc));
        
        
        
    

    Real always returns 0
    Long always returns 0
    float returns the correct answer of 20
    So whats the difference between Real and float? Is it ok to use float instead of Real?
    I don't know anything about a Mac, but Iv'e always assumed Real was there to use so everything is platform independant, because I thought a float wasn't?
    This has confused me, because I realy need to return a percentage of a number and use it for StatusSetBar(), but how can I do this if LONG and Real datatypes don't return the correct results



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 26/02/2004 at 06:04, xxxxxxxx wrote:

    Hi Geespot,
    I don't think that there's a diffrence between Real and float but there's a difference in your calculation:

    try this and you will always get 20:

      
    Real a = 10; //instead of LONG a = 10;  
    Real b = 50; //instead of LONG b = 50;  
    Real rperc = a/b*100;  
    LONG lperc = a/b*100;  
      
    float fa = 10;  
    float fb = 50;  
    float fperc = fa/fb*100;  
                           
    GePrint("Real: " + RealToString(rperc));  
    GePrint("Long: " + LongToString(lperc));  
    GePrint("Float: " + RealToString(fperc));  
    

    Dividing a LONG value through another LONG value causes a rounding. In your example 10/50 == 0.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 26/02/2004 at 14:43, xxxxxxxx wrote:

    Hi iwaitz,
    The a and b variables were just examples, and my a and b variables I use in my plugin are in LONG and I can't change that because they are returned by certain functions within the SDK.
    But I found out why, it was because the LONG a,b variables were rounding the result to 0, just like you said. A simply cast did the trick

        
        
        
        
        Real rperc = (Real)a/(Real)b*100;
        
        
        
    

    I also found in the ge_win_math.h header this:

        
        
        
        
        typedef float        Real;
        
        
        
    

    so Real's and float are exactly the same. I suppose MAXON used Real to keep it friendly with the C4D interface.
    I must say, it was the first time I looked at that header file, it's an interesting file because it explains the datatypes, ie

        
        
        
        
        #define pi  3.1415926535897932384626433832795  
        
    

    I guess MAXON wanted to have PI really precise :)



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 26/02/2004 at 15:18, xxxxxxxx wrote:

    That´s not really precise as PI has endless numbers after the decimal place ;)


Log in to reply