primitive range

R

red floyd

John said:
Is there a guaranteed minimum size for the primitive double?

I need to know if I can store 5.12 * 2^32 - 1 in a double.

http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.5

Only talks about minimum sizes for integer values. It doesn't say
anything about float or double.

Thanks,

--John Ratliff

The standard says that the rep for a floating point value is
implementation defined (3.9.1/8). It also says to use
std::numeric_limits<> (18.2) for any given type.

However most implementations of double that I've seen use IEEE 64 bit
rep (1 sign, 10 bit exponent, 53 bit normalized mantissa). The real so
a double probably could. The other issue is that I'm not sure that 5.12
is exactly representable as a double (It may be an infinitely recurring
binary fraction).
 
J

John Ratliff

red said:
The standard says that the rep for a floating point value is
implementation defined (3.9.1/8). It also says to use
std::numeric_limits<> (18.2) for any given type.

However most implementations of double that I've seen use IEEE 64 bit
rep (1 sign, 10 bit exponent, 53 bit normalized mantissa). The real so
a double probably could. The other issue is that I'm not sure that 5.12
is exactly representable as a double (It may be an infinitely recurring
binary fraction).

Okay. This is my test progam. I get the right value, but I want to make
sure my conversion is okay.

#include <cmath>
#include <iostream>

int main(int, char **) {
double factor = 256.0 / 50.0;
double time = (0xFFFFFFFF * factor);
double temp;

temp = time / 3600;
std::cout << "hours = " << static_cast<int>(temp) << std::endl;

temp = fmod(time / 60, 60);
std::cout << "minutes = " << static_cast<int>(temp) << std::endl;

temp = fmod(time, 60);
if ((temp - floor(temp)) >= 0.5) ++temp;
std::cout << "seconds = " << static_cast<int>(temp) << std::endl;

return 0;
}

When I round the seconds, will that be valid, or do I need to do
something more like:

if ((floor((temp - floor(temp)) * 10) / 10) >= 0.5) ++temp;

Thanks,

--John Ratliff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top