Round in C

How do you round in C?

Page 1 of 1

8 Replies - 22373 Views - Last Post: 02 November 2005 - 08:18 PM Rate Topic: -----

#1 jaredolympia  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 jaredolympia  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 Senger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 Senger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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....
Was This Post Helpful? 0
  • +
  • -

#6 jaredolympia  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 Senger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!)
Was This Post Helpful? 0
  • +
  • -

#8 jaredolympia  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • 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).
Was This Post Helpful? 0
  • +
  • -

#9 Senger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1