rounding

gerald
06-04-2008, 10:31 PM
Hello everybody me again.

So I have a table of values and I need to round them off

for example 192293.577 should become 192293.58

and 2113283.34 should stay the same

thanks!!!

gerald

PS: I now have a fortran book but it does not say anything about rounding. I tried ANINT but i don't think that's what i want.

davekw7x
06-04-2008, 11:31 PM
So I have a table of values and I need to round them off
Do you need to round off the values to the nearest floating point number with a specified number of decimal places or do you just want to print the values out to a specified number of decimal places? I'll show ways that try both approaches.

Remember: With binary floating point internal representation, you might not be able to store an exact value of a decimal floating point number, but the I/O routines can be made to show a certain number of decimal places, and they will round them off as required. (Usually.) See Footnote.

I tried ANINT...

ANINT will round off a floating point number to the nearest integer value.

If you want to round off a floating point number to two decimal places, you can try the following:

1. Multiply by 100.0
2. Use ANINT to round to the nearest integer value
3. Divide by 100.0

You could do this in one statement, but I do it in a couple of steps below, in hopes that it might make sense. (You can also print out the intermediate value if you want to.)

program ShowRounding

implicit none
double precision :: x, y
integer(kind=8) :: ii, jj
x = 192293.577d0
y = 2113283.34d0

write(*,*) " Originally:"
write(*,*) " Unformatted : x = ", x
write(*,*) " Unformatted : y = ", y
write(*,'(" With f12.2 format: x = ", f12.2)') x
write(*,'(" With f12.2 format: y = ", f12.2)') y

ii = ANINT(x*100.0) !Multiply by 100 and round to int
x = ii / 100.0 !Floating point divide by 100

jj = ANINT(y*100.0) !Multiply by 100 and round to int
y = jj / 100.0 !Floating point divide by 100

write(*,*) " After rounding:"
write(*,*) " Unformatted : x = ", x
write(*,*) " Unformatted : y = ", y
write(*,'(" With f12.2 format: x = ", f12.2)') x
write(*,'(" With f12.2 format: y = ", f12.2)') y

end program ShowRounding

Output, using GNU gfortran version 4.1.2 on my Linux system:

Originally:
Unformatted : x = 192293.577000000
Unformatted : y = 2113283.34000000
With f12.2 format: x = 192293.58
With f12.2 format: y = 2113283.34
After rounding:
Unformatted : x = 192293.580000000
Unformatted : y = 2113283.34000000
With f12.2 format: x = 192293.58
With f12.2 format: y = 2113283.34

The first statements don't actually change the internal values of the variables, they let the I/O do the "rounding."

The second group shows values after the values have been "rounded" with the little trick using ANINT.

Regards,

Dave

Footnote: If you are going to insist on exact values of your decimal floating point numbers from your binary floating point calculations, there are times in your life that you will surely be disappointed, as roundoff errors from initial values are compounded by roundoff errors in representing intermediate values, etc., etc.

In particular if you have a table of values of decimal numbers rounded to a particular number of decimal places (after some calculations using binary floating point arithmetic), you must proofread it very carefully to make sure that some sneaky accumulation of roundoff errors hasn't altered some least-significant digit somewhere.

Bottom line: foating point numbers are all about 'precision' or 'number of significant digits', not about 'accuracy' or 'number of decimal places.' Maybe that's why many books don't dwell on details like rounding floating point number to a specified number of decimal places.

gerald
06-06-2008, 12:19 AM
:o:o I was using the same trick but I had declared x and y as real. Now i'm getting the answer that i wanted.

Thanks!!!

gerald