C++ Primer ex 7.3

B

BobR

arnuld said:
i think, -2147483648 is too big to fit in a double.

WHAT?

int big( -2147483648 ); // int is 32bit on my machine
cout<<" big="<<big<<std::endl;
// out: big=-2147483648


// include <limits>

std::cout<<" dbl min() ="
<< std::numeric_limits<double>::min()<<std::endl;

std::cout<<" dbl max() ="
<< std::numeric_limits<double>::max()<<std::endl;

std::cout <<" dbl digits ="
<<(std::numeric_limits<double>::digits)<<std::endl;

std::cout<<" LD digits ="
<<(std::numeric_limits<long double>::digits)<<std::endl;

/* - output - (on my machine)
dbl min() =0.000000 // note: only precision(6)
dbl max() =1.79769e+308
dbl digits =53 // 'digits' is num of bits
LD digits =64
*/

Compare those 'digits' with int, long, long long(if you have it) digits.

When you are not sure, test it.
 
J

James Kanze

i think, -2147483648 is too big to fit in a double.

Are you kidding. On most machines today, a double will handle
anything up to around 1e306, with 17 digits precision.

In my version, the arguments were int, and the above value fits
into an int on all of the machines I currently work on; it is
guaranteed (by the standard) to fit into a long or a double.
then you can also use these arguments:

but why i need such big values ? may be they are specific
requirements for some specific domain like scientific
programming, ....... why do i even need -2147483648 ?

I don't know? But you do want it to work with all legal values.

In fact, of course, I just raised the point because someone
mentionned accepting negative values. Your orginal code was
probably all the book was looking for, but at some point, you
will have to learn the awkward particularity that on most
machines, the absolute value of the most negative value of an
integral type will not fit in that integral type. Which means
that you often have to pay a little extra attention.
i think just making it work for values which fit in a double is ok.

-2147483648 definitly fits into a double. With no loss of
precision on most machines.
 

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

Similar Threads

C++ Primer ex 7.6 17
C++ Primer ex 6.20 36
C++ Primer ex 8.3 21
C++ Primer ex 4.30 10
C++ Primer ex 9.27 4
C++ Primer ex 7.20 - finding factorial 13
C++ Primer ex 7.14 2
C++ Primer ex 7.16 - arguments to main 15

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top