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.
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!)
Edited 6 Years Ago by DoctorBinary: changed word "will" to "can"
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.
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 :)).
Need some help with this Array. I am trying to get the sum of the even numbers and the sum of the odd numbers using a for each loop. I know the answers to what I am trying to achive are sum of even = 84 and the sum of ...
I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...