I need to get character input from the user and convert it into a float type. However, I can't use arrays, strings or the functions printf() and scanf(). I have to do everything with putchar() and getchar(). How would I do this?
I need to get character input from the user and convert it into a float type. However, I can't use arrays, strings or the functions printf() and scanf(). I have to do everything with putchar() and getchar(). How would I do this?
initialize a float with value zero.
begin a while loop to take character input, and break on newline entered.
in the body of the while loop you will :
--- check if decimal point is entered. if so, keep track of this fact, and continue to get next character
--- convert numeric char to value
--- if value is prior to decimal point, multiply total float value by 10, then add the numeric value
--- if after the decimal point, divide the numeric value by incrementing powers of 10, add the result to the total float value
you will need to perform error checking at all inputs, to ensure no invalid characters are entered.
Thank You! it worked perfectly!
awesome. i'm surprised you were able to interpret my description so quickly.
if after the decimal point, divide the numeric value by incrementing powers of 10, add the result to the total float value.
Keep in mind that dividing by powers of 10 can result in some roundoff error, meaning the result may not be the closest floating-point value to the input. (I'd imagine though that for the purposes of the questioner's assignment, this will suffice; performing correctly rounded conversion requires a lot of code!)
yeah, and i should have specified that the values be all declared as type "double" lloating point. it's kind of assumed, but many people dont know or forget.
why should they all be double? it seems to work fine with float
yeah, and i should have specified that the values be all declared as type "double" lloating point.
Although my comment still stands, regardless of float, double, or even "double extended" precision.
why should they all be double? it seems to work fine with float
Using either floats or doubles will work "fine", if by "fine" you mean not necessarily correctly rounded. Using doubles will make them less incorrectly rounded.
why should they all be double? it seems to work fine with float
because type "float" has really poor precision, and is not sufficient for performing any significant floating point calculations as the rounding errors will stack and quickly become unacceptable. type "double" has much greater precision and the rounding errors will be much less noticeable. "double" is generally considered the default floating point type that you should always use, at a minimum. there is really no good reason to use "float". memory size is inconsequential.
and yes, Dr.B is correct about the entire method being susceptible to rounding errors. because the successive divides by 10 have inherent rounding error ... it's nice to be aware of it, but it will not affect your assignment. like most "doctors", Dr.B has led us into a pedantic discussion, that is tangential to your assingment. :)
don't worry about these details as far as turning in your homework. this is all beyond the scope of your assignment.
.
like most "doctors", Dr.B has led us into a pedantic discussion, that is tangential to your assingment. :)
.
Of course they're real issues to be aware of, should cokaznsyco72 continue on to program floating-point in real applications.
Of course they're real issues to be aware of, should cokaznsyco72 continue on to program floating-point in real applications.
but of course
cokaznsyco, dr.b is correct that you need to be aware that programming floating point operations have an inherent issue with rounding errors. just like 1/3 has rounding problems in decimal arithmetic, 1/10 has rounding problems in binary arithmetic.
but when you do real applications with real floating point problems, you will also be using real functions from real standard libraries, and not these "getchar/putchar" stupid programming tricks your instructor is having you to do.
so, i'm just sayin', don't get too worried about it yet. :)
.
you will also be using real standard libraries and real functions, and not these "getchar/putchar" stupid programming tricks your instructor is having you to do.
Point taken. Correct rounding should be done by the compiler/libraries (strtod, etc.), so you shouldn't need to worry about it (well, some implementations still don't get it quite right, but that's a pedantic discussion for another day :)).
thanks for all your help!
I elaborated on my thoughts of this topic in an article I just published: http://www.exploringbinary.com/quick-and-dirty-decimal-to-floating-point-conversion/. Please check it out!