Okay, I have Biology teacher that requires students to express their answers to the nearest hundredth. I need an efficient algorithm that can round a type double variable to the nearest hundredth.
Sincerely,
jaredolympia
8 Replies  22648 Views  Last Post: 02 November 2005  08:18 PM
Replies To: Round in C
#2
Re: Round in C
Posted 02 November 2005  06:52 PM
Here are some hints:
You need to look at the digit in the 3rd decimal place (the thousandths digit).
If it's less than 5, don't touch the hundredths digit.
It it's equal to or greater than 5, you need to increment the hundredths digit (which means that if the hundredths digit is a 9, there's going to be a rollover effect to the tenths digit, and so on)...or more simply, add .01 to the number.
You need to look at the digit in the 3rd decimal place (the thousandths digit).
If it's less than 5, don't touch the hundredths digit.
It it's equal to or greater than 5, you need to increment the hundredths digit (which means that if the hundredths digit is a 9, there's going to be a rollover effect to the tenths digit, and so on)...or more simply, add .01 to the number.
#3
Re: Round in C
Posted 02 November 2005  06:57 PM
I know you have to look at the thousands digit (we humans can do that mentally); however, how can you look at the thousands digit of a type double? The actual number is in binary which is where my problem surfaces.
#4
Re: Round in C
Posted 02 November 2005  07:01 PM
Oh. Is that the task, to write an algorithm for rounding a float stored as a binary number, with the information on the decimal being encoded rather than being explicitly displayed?
You may want to take a look at this page (I searched Google for these terms: floating point representation):
http://www.nuvisionm...loating_tut.htm
You may want to take a look at this page (I searched Google for these terms: floating point representation):
http://www.nuvisionm...loating_tut.htm
#5
Re: Round in C
Posted 02 November 2005  07:05 PM
If it's not a question of manipulating an IEEE floatingpoint binarythat is, if you have a number in decimal form to work withI wonder if this would work:
number % .01
or:
(number * 1000) % 10
Sorry if I'm giving useless suggestions here....
number % .01
or:
(number * 1000) % 10
Sorry if I'm giving useless suggestions here....
#6
Re: Round in C
Posted 02 November 2005  07:15 PM
Thanks for devoting some of your time. That does help me get started.
#7
Re: Round in C
Posted 02 November 2005  07:50 PM
Was it the first approach (IEEE representation) or the second one (modulo arithmetic) that helped you?
I'm just wondering why a biology teacher would expect his/her students to even know about binary representation.... (Great if you're a comp sci major, though!)
I'm just wondering why a biology teacher would expect his/her students to even know about binary representation.... (Great if you're a comp sci major, though!)
#8
Re: Round in C
Posted 02 November 2005  07:57 PM
The IEEE representation helped the most. To be honest, our Biology teacher is making us do standard deviation, so out of laziness, I decided to make a program that computes and graphs the data according to her standards (she's pretty picky).
#9
Re: Round in C
Posted 02 November 2005  08:18 PM
I wouldn't say that's lazy, but rather resourceful. If you were simply bypassing doing the work, yes, it would be lazy. But working out the algorithm and writing a program for it should teach you everything you need to know about computing standard deviation!
Page 1 of 1
