Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round a double to the closest and greater float

I want to round big double number (>1e6) to the closest but bigger float using c/c++. I tried this but I'm not sure it is always correct and there is maybe a fastest way to do that :

int main() {
    // x is the double we want to round
    double x = 100000000005.0;
    double y = log10(x) - 7.0;
    float a = pow(10.0, y);
    float b = (float)x;

    //c the closest round up float
    float c = a + b;
    printf("%.12f %.12f %.12f\n", c, b, x);
    return 0;
}

Thank you.

like image 455
user1482030 Avatar asked Mar 08 '13 12:03

user1482030


People also ask

Is 1.5 float or double?

And the reason the comparison succeeds with 1.5 is that 1.5 can be represented exactly as a float and as a double ; it has a bunch of zeros in its low bits, so when the promotion adds zeros the result is the same as the double representation.

How do you round a double in Swift?

We can round a double to the nearest Int by using the round() method in Swift. If the decimal value is >=. 5 then it rounds up to the nearest value. for example, it rounds the 15.7 to 16.0 .

Why are floats faster than doubles?

Floats are faster than doubles when you don't need double's precision and you are memory-bandwidth bound and your hardware doesn't carry a penalty on floats. They conserve memory-bandwidth because they occupy half the space per number. There are also platforms that can process more floats than doubles in parallel.

What is a float vs double?

A float has 7 decimal digits of precision and occupies 32 bits . A double is a 64-bit IEEE 754 double-precision floating-point number. 1 bit for the sign, 11 bits for the exponent, and 52 bits for the value. A double has 15 decimal digits of precision and occupies a total of 64 bits .


3 Answers

Simply assigning a double to float and back should tell, if the float is larger. If it's not, one should simply increment the float by one unit. (for positive floats). If this doesn't still produce expected result, then the double is larger than supported by a float, in which case float should be assigned to Inf.

float next(double a) {
    float b=a;
    if ((double)b > a) return b;
    return std::nextafter(b, std::numeric_limits<float>::infinity());
}

[Hack] C-version of next_after (on selected architectures would be)

float next_after(float a) {
    *(int*)&a += a < 0 ? -1 : 1;
    return a;
}

Better way to do it is:

float next_after(float a) {
   union { float a; int b; } c = { .a = a };
   c.b += a < 0 ? -1 : 1;
   return c.a;
}

Both of these self-made hacks ignore Infs and NaNs (and work on non-negative floats only). The math is based on the fact, that the binary representations of floats are ordered. To get to next representable float, one simply increments the binary representation by one.

like image 159
Aki Suihkonen Avatar answered Oct 08 '22 16:10

Aki Suihkonen


If you use c99, you can use the nextafterf function.

#include <stdio.h>
#include <math.h>
#include <float.h>

int main(){
  // x is the double we want to round
  double x=100000000005.0;

  float c = x;

  if ((double)c <= x)
    c = nextafterf(c, FLT_MAX);

  //c the closest round up float
  printf("%.12f %.12f\n",c,x);
  return 0;
}
like image 41
Mankka Avatar answered Oct 08 '22 16:10

Mankka


C has a nice nextafter function which will help here;

float toBiggerFloat( const double a ) {
    const float test = (float) a;
    return ((double) test < a) ? nextafterf( test, INFINITY ) : test;
}

Here's a test script which shows it on all classes of number (positive/negative, normal/subnormal, infinite, nan, -0): http://codepad.org/BQ3aqbae (it works fine on anything is the result)

like image 30
Dave Avatar answered Oct 08 '22 16:10

Dave