# Round in C

Page 1 of 1

## 8 Replies - 23185 Views - Last Post: 02 November 2005 - 08:18 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=13078&amp;s=36fcb1ae8a6e898e48cf387f48002212&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jaredolympia

Reputation: 3
• Posts: 29
• Joined: 15-October 05

# Round in C

Posted 02 November 2005 - 06:25 PM

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
Is This A Good Question/Topic? 0

## Replies To: Round in C

### #2 Senger

Reputation: 0
• Posts: 9
• Joined: 02-November 05

## 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.

### #3 jaredolympia

Reputation: 3
• Posts: 29
• Joined: 15-October 05

## 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 Senger

Reputation: 0
• Posts: 9
• Joined: 02-November 05

## 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

### #5 Senger

Reputation: 0
• Posts: 9
• Joined: 02-November 05

## Re: Round in C

Posted 02 November 2005 - 07:05 PM

If it's not a question of manipulating an IEEE floating-point binary--that is, if you have a number in decimal form to work with--I wonder if this would work:

number % .01

or:

(number * 1000) % 10

Sorry if I'm giving useless suggestions here....

### #6 jaredolympia

Reputation: 3
• Posts: 29
• Joined: 15-October 05

## 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 Senger

Reputation: 0
• Posts: 9
• Joined: 02-November 05

## 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!)

### #8 jaredolympia

Reputation: 3
• Posts: 29
• Joined: 15-October 05

## 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 Senger

Reputation: 0
• Posts: 9
• Joined: 02-November 05

## 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!