CGFloat: round, floor, abs, and 32/64 bit precision

后端 未结 2 693
挽巷
挽巷 2021-01-04 10:16

TLDR: How do I call standard floating point code in a way that compiles both 32 and 64 bit CGFloats without warnings?


CGFloat is defined as either double or fl

相关标签:
2条回答
  • 2021-01-04 10:55

    If you only need a few functions you can use this instead:

    #if CGFLOAT_IS_DOUBLE
    #define roundCGFloat(x) round(x)
    #define floorCGFloat(x) floor(x)
    #define ceilCGFloat(x) ceil(x)
    #else
    #define roundCGFloat(x) roundf(x)
    #define floorCGFloat(x) floorf(x)
    #define ceilCGFloat(x) ceilf(x)
    #endif
    
    0 讨论(0)
  • 2021-01-04 11:04

    You may use those functions from tgmath.h.

    #include <tgmath.h>
    
    ...
    
    double d = 1.5;
    double e = floor(d);   // will choose the 64-bit version of 'floor'
    
    float f = 1.5f;
    float g = floor(f);    // will choose the 32-bit version of 'floorf'.
    
    0 讨论(0)
提交回复
热议问题