c++ - Comparisons of arbitrary arithmetic types: does anyone know an implementation? -



c++ - Comparisons of arbitrary arithmetic types: does anyone know an implementation? -

while writing several math utilities bumped need implement generic utility can perform comparisons between 2 fundamental arithmetic types. began coding, became clear operation not straightforward seems, since need right handling of corner cases, when types have different precision, i.e. rounding strategy during conversion between types becomes important. consider:

float a1 = 4.8f; int a2 = 4; assert(a2 != (int) a1); //fails erroneously since truncated a1 float b1 = 40000000.0f; //can represent 40000000 , 40000004 accurately long b2 = 40000002; assert(b1 != (float) b2); //fails erroneously since truncated b2

the above can implemented using c++0x type traits automatically select appropriate algorithm according template arguments supplied comparing function. however, quite complex , there's quite lot of places bugs can creep, don't think inventing myself worthwhile. know library implements above correctly?

you may want gnu's mp bignum library @ http://gmplib.org/. page:

gmp free library arbitrary precision arithmetic, operating on signed integers, rational numbers, , floating point numbers. there no practical limit precision except ones implied available memory in machine gmp runs on. gmp has rich set of functions, , functions have regular interface.

gmp designed fast possible, both little operands , huge operands. speed achieved using fullwords basic arithmetic type, using fast algorithms, highly optimised assembly code mutual inner loops lot of cpus, , general emphasis on speed.

c++ comparison c++11 numeric

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -