Learning C - Scanf or Getch, or Getchar not working correctly after first loop.

U

user923005

Input is one of the most basic things that a program has to do.
It is also inherently difficult if the programmer does not control what is
entered.

For instance the input 100000000000000000000000 will overflow an integer and
be approximated in a floating point type. However it is a number, it may
even be a valid number for the problem domain. So you have to be very
careful in what you tell the user.

100000000000000000000000 will be represented exactly in any reasonable
floating point type.
There is only one significant digit.

Now, 100000000000000000000001 is another story.
 
B

Ben Pfaff

user923005 said:
100000000000000000000000 will be represented exactly in any reasonable
floating point type.
There is only one significant digit.

There is only one significant digit in base 10. In base 2, there
are 54 significant bits:
2#10101001011010000001011000111111000010100101011110110100000000000000000000000
That's near the edge of what you can expect to get out of a
64-bit floating-point number.
 
J

Joe Wright

Ben said:
There is only one significant digit in base 10. In base 2, there
are 54 significant bits:
2#10101001011010000001011000111111000010100101011110110100000000000000000000000
That's near the edge of what you can expect to get out of a
64-bit floating-point number.

That would be 53 bits I think for a double. A sign bit, 11 exponent bits
and 53 mantissa bits add to 65. Because bit 52 of the mantissa is always
1, its position is given up to bit 0 of the exponent. Voila, 64 bits.
 
P

pflink

For anyone interested, I went to my local library and got "The C
Programming Language" by Kernighan and Ritchie. Scanf is not introduced
until chapter 7, so I guess I answered my own question. :D It's a little
old, but with running linux, so far the standard library seems to be the
same.

Peter
 
C

CBFalconer

For anyone interested, I went to my local library and got "The C
Programming Language" by Kernighan and Ritchie. Scanf is not
introduced until chapter 7, so I guess I answered my own question.
:D It's a little old, but with running linux, so far the standard
library seems to be the same.

It is, together with the errata, up-to-date for C90. You will have
nothing to unlearn later.

Please don't top-post. Your answer belongs after (or intermixed
with) the material you quote, after snipping anything irrelevant.
See the following links.

--
Some informative links:
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/> (taming google)
<http://members.fortunecity.com/nnqweb/> (newusers)
 

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,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top