Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

accuracy in gfortran

Status
Not open for further replies.

zmth

Technical User
Aug 5, 2015
57
PH
if all the variables at first are integer isn't integer multiplication supposed to be exact ?
Even though i am getting very large numbers(as long as not overflow): anyway at the end i will have to divide a floating point number by the integer(s). There are several steps and at first i just divided the floating point number by the integers at each step and was getting more off than what i should have thought reasonable so then i changed strategy and multiplied all the integers and at the end divided by the product.Assuming integer multiplication is exact would not the 2nd way of doing all the integer multiplications first and then divide the floating point number by the final(rather large but not enough to overflow or underflow) result be more accurate than doing several divisions of the floating point number by the integers at each step.
The difference in these two ways was a lot more than i would have thought - like around 15% or so.
The floating point was single precision and the number of integer products was less than 20 i think. The reason i would have thought the 2nd way is better is because only have one floating point division by an integer(all be it much larger) instead of several.
 
It does depend on what the actual mathematical calculation is. Integer division can change the result dramatically

Take some simple addition

(28 / 10) + (29 /10)
= 2 + 2
= 4

but

(28 + 29) / 10
= 57 / 10
= 5

With multiplication

(24 / 10) * (3 / 2)
= 2 * 1
= 2

But

(24 * 3)/(10 * 2)
= 72 / 20
= 3
 
You are dividing integer by integer. I don't have that case at all. I am only multiplying integer times integer and at the end dividing floating point by the integers product which i presume should be more accurate or perhaps at worst same? than dividing the float by each integer at a time rather than waiting till the end and dividing only once by the integers product.
 
Are you dividing by float or double precision? Floats only have 6 digit accuracy. Also - it is just significant figures: not decimal places. The closer you are to 1, the more accurate the answer.

Dividing may produce a different result to multiplying by the reciprocal. Mathematically, it is the same. Work out on a calculator or by hand what the answer should be and check the two methods.
 
I was dividing floats by integers. Yea I know am going to get round off,floating point error no matter what. I was just asking if generally one way was better than the other. That is dividing floats as the program goes by one or smaller number of integers as i go or wait till at the end and then then divide the final float number by the product of all the integers in one final step.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top