Problem with precision / rounding in Fortran [duplicate] - fortran

This question already has answers here:
Assigning a lower precision number to a higher precision in Fortran90
(1 answer)
Numerical Precision in Fortran 95:
(2 answers)
Closed 4 years ago.
I'm trying to understand the difference between these two:
REAL*8 X
X=1.5D0
WRITE(6,*) (0.6D0 + (2*x)/5.)
WRITE(6,*) (0.6 + (2*x)/5.D0)
I would expect them to give identical results, but I get instead
1.2000000000000000
1.2000000238418580
Why is 0.6 not cast to double precision in the second case even if it is supposed to be summed to a value in double precision? What is happening?

Related

How to write fraction instead of decimal in C++? [duplicate]

This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
Closed 3 years ago.
I want to write 4/3 as a fraction in C++. I know that 4/3 is equal to 1.33333. But when I write 4/3 in the code it outputs it's quotient which is 1. Can anyobody tell me how to write this number as fraction?
The code:
double vol_sphere(double radiusS){
return (4/3) * pi * pow(radiusS, 3);
}
Because 4 and 3 are both integers, when you perform division of two integers, the result will be also integer, so 1.333333333 will be only 1.

Slight change of double value in C++ [duplicate]

This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 7 years ago.
The value of d which is a double when compared to 0.01 is not satisfied due to presence of some digits at the end of the number which I found using flags . Why does this occur and how do we overcome this ? Thank you .
Code for this : http://i.stack.imgur.com/06I81.png
if ( abs( d - 0.01 ) < 0.0000001 ) ...

Fortran calculate sum of floating numbers not getting exact precision [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
I am new to Fortran90 , I have write a simple program to add two floating point numbers as follows:
program Numbers_sum
implicit none
REAL :: sum
sum = 1.6+2
print*,"Sum =", sum
end
I am getting the answer as Sum = 3.5999999
Why it is not getting 3.6. How Can I make this program to get the exact answer?? Any help will be appreciated.
There is no way to write 3.6 in base 2 with finite digits. It's 2 + 1 + 1/2 + 1/16 + ...
You can, however, hide the rounding error by selecting proper formatting:
write(*, '(F11.6)') sum
If you want to calculate in higher precision, you could use this:
REAL(KIND=8) :: var
Or, if you want to be really proper:
program numbers_sum
implicit none
integer, parameter :: dp = selected_real_kind(P=12)
real(kind=dp) :: sum1
sum1 = 1.6_dp + 2
print *, "Sum = ", sum1
end
But even this won't eliminate the rounding completely.
Cheers

Problems while dividing by 100 [duplicate]

This question already has answers here:
Division in C++ not working as expected
(6 answers)
Closed 8 years ago.
Helo, I'm new to programming and run into an issue, I have an integer, for example 158, and I divide it by 100 that i get is 1, but I want 1.58 instead
It is probably known issue, but sorry, I'm noob, for now :)
Just cast this to float number
int i = 158;
float f = (float)i / 100; //less precision
double d = (double)i / 100; //more precision
//other way
int i = 158;
float f = i / 100.0; //less precision
double d = i / 100.0; //more precision
What you are doing is dividing integer from integer, in this case result always integer, to get floating point number at least one of two operand has to be floating point number.
You need to divide by 100.0 rather than 100
Dividing by an integer in C++ is always going to give you an integer, so it will never be completely accurate. That being said, it was mentioned above that you can divide by a double or long to get the accurate decimal number that you desire.

Why can't I output a basic double in c++ [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Incorrect floating point math?
Using C++ console app in visual studio.
float percentCorrect;
percentCorrect = ( 7 / 5 );
printf("%f", percentCorrect);
output is 1.0000
OK so how do I get the correct decimal output??
7 and 5 are integers, so the compiler treats them as such and does integer division.
float percentCorrect;
percentCorrect = (7.0/5.0);
printf("%f", percentCorrect);
The above should print out the expected value.
You need to include a decimal: 7.0/5.0
7 and 5 are integer literals, so 7 / 5 performs integer division.
You need to have at least one floating literal for floating-point division to be performed:
float percentCorrect = 7.0 / 5.0;