views:

131

answers:

5

Hi, I would like to have the closest number below 1.0 as a floating point. By reading wikipedia's article on IEEE-745 I have managed to find out that the binary representation for 1.0 is 3FF0000000000000, so the closest double value is actually 0x3FEFFFFFFFFFFFFF.

The only way I know of to initialize a double with this binary data is this:

double a;
*((unsigned*)(&a) + 1) = 0x3FEFFFFF;
*((unsigned*)(&a) + 0) = 0xFFFFFFFF;

Which is rather cumbersome to use.

Is there any better way to define this double number, if possible as a constant?

+3  A: 

It's not safe, but something like:

double a;
*(reinterpret_cast<uint64_t *>(&a)) = 0x3FEFFFFFFFFFFFFFL;

However, this relies on a particular endianness of floating-point numbers on your system, so don't do this!

Instead, just put DBL_EPSILON in <cfloat> (or as pointed out in another answer, std::numeric_limits<double>::epsilon()) to good use.

Oli Charlesworth
Treating it as an integer should make it endian-independent (unless you have one of those weird "mixed endian" systems)
Rick Regan
@Rick Regan: Who's to say that the "endianness" of your platform's representation of floating-point types is consistent with the representation of integer types?
Oli Charlesworth
@Oli Charlesworth Theoretically you're right -- but do you have an example (besides the mixed-endian “soft floats”)?
Rick Regan
+3  A: 
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;

int main()
{
    double const    x   = 1.0 - numeric_limits< double >::epsilon();

    cout
        << setprecision( numeric_limits< double >::digits10 + 1 ) << fixed << x
        << endl;
}

Cheers & hth.,

Alf P. Steinbach
+1  A: 

If you make a bit_cast and use fixed-width integer types, it can be done safely:

template <typename R, typename T>
R bit_cast(const T& pValue)
{
    // static assert R and T are POD types

    // reinterpret_cast is implementation defined,
    // but likely does what you expect
    return reinterpret_cast<const R&>(pValue);
}

const uint64_t target = 0x3FEFFFFFFFFFFFFFL;
double result = bit_cast<double>(target);

Though you can probably just subtract epsilon from it.

GMan
Mark Ransom
@Mark: That wouldn't static assert both types are POD types, and would make it easier to break aliasing rules. (Admittedly I gave a more general solution than required; in this case just doing it directly works fine.)
GMan
A: 

It's a little archaic, but you can use a union. Assuming a long long and a double are both 8 bytes long on your system:

typedef union { long long a; double b } my_union;

int main()
{
    my_union c;
    c.b = 1.0;
    c.a--;
    std::cout << "Double value is " << c.b << std::endl;
    std::cout << "Long long value is " << c.a << std::endl;
}

Here you don't need to know ahead of time what the bit representation of 1.0 is.

socket puppet
That leads to UB, strictly speaking.
GMan
+3  A: 

Hexadecimal float and double literals do exist. The syntax is 0x1.(mantissa)p(exponent in decimal) In your case the syntax would be

double x = 0x1.fffffffffffffp-1
Shum
I've never heard of this syntax before. Do you have a reference?
Mark Ransom
I think its part of the C99 standard. It works with GNU compilers, I don't about others.
Shum
@Mark Ransom: I wrote an article about this recently: http://www.exploringbinary.com/hexadecimal-floating-point-constants/
Rick Regan
@Mark Ransom: Added in C99. Also supported in `printf`/`scanf` via the `%a` format specifier. By far the best way to specify floating-point values in C.
Stephen Canon