Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29282 Discussions

why the result of CVF and CPP not equal

Intel_C_Intel
Employee
1,160 Views
CVF and PGF90:
real*8 rs
rs=dcos(dsin(1100000.0))
! rs=0.927451417863706
CPP(g++):
double i;
i=1100000.0;
i=cos(sin(i));

// i=0.999999
what's the matter?
Thank!
0 Kudos
7 Replies
Intel_C_Intel
Employee
1,160 Views
FORTRAN:
real*8 x(10),y(10),i
integer j
x=(/(1.1*j,j=1,10)/)
do i=1.0,10000.0,0.1
y=dcos(dsin(x*i))
enddo
print*,y
print*,"---------------------------------------"
print*,"---------------------------------------"
x=(/(1.1d0*j,j=1,10)/)
do i=1.0d0,10000.0d0,0.1d0
y=dcos(dsin(x*i))
enddo
print*,y
end

Result:
0.603824893601652 0.756962151192525 0.930925120912308
0.540388345095186 0.920971720348514 0.769015433909829
0.595637597164346 0.999591124599520 0.611723873042041
0.742800902664028
---------------------------------------
---------------------------------------
0.574184213326437 0.856445471589781 0.794619918650350
0.612629194927815 0.990632673212321 0.549625052870082
0.912164220137757 0.731635495768517 0.662642071205611
0.963401666343262

CPP:
#include
#include
void main()
{
double x[11],y[11],i;
int j;
for(j=1;j<=10;j++) x=1.1*j;
for(i=double(1.0);i<=double(10000.0);i+=double(0.1))
{
for(j=1;j<=10;j++)
{
y=cos(sin(x*i));
}
}
for(j=1;j<=10;j++) printf("%f ",y);
getchar();
}

Result:
0.603949 0.756593 0.931736 0.540409 0.920679 0.771226 0.594482 0.999494 0.614068 0.742055
0 Kudos
Intel_C_Intel
Employee
1,160 Views
I think that CVF is getting the answer correct. In that case the question is (to ask on a C++ forum?) is why G++ is getting it wrong. This may narrow things down a bit.

Joe
0 Kudos
Intel_C_Intel
Employee
1,160 Views
Thank you!
Can you tell me wnhy you think so,just for you think my code is right?
But I think the c++ code is right too.

I will ask on c++ forum as you said.
thanks!
0 Kudos
david_jones
Beginner
1,160 Views
It would be illuminating to (either or both):
(i) count in each case how many times the i-loop is actually transited;
(ii) print out, to as many dp as needed, the actual last value of i that is used.

A guess is that there will be differences because of the use of a non-integer loop control variable with a step that is not exactly computer-representable. You could potentially find that changing compiler options regarding code generation/optimisation would lead to different answers even keeping the code/language the same.
0 Kudos
Intel_C_Intel
Employee
1,160 Views
thank
i change my code:
f90:
do i=1.0d0,100.0d0,0.10000000d0
c++:
for(i=double(1.0);i<=double(100.0);i+=double(0.10000000))
the result of i on pgf90 or pgCC:

f90:
4.200000000000002
4.300000000000002
4.400000000000001
4.500000000000001
4.600000000000001
4.700000000000000
4.800000000000000
4.899999999999999
4.999999999999999
5.099999999999999
5.199999999999998
5.299999999999998

c++:
4.80000000000000
4.90000000000000
5.00000000000000
5.10000000000000
5.20000000000000
5.30000000000000
5.40000000000000
5.50000000000000
5.60000000000000
5.70000000000000
5.80000000000000
5.90000000000000
6.00000000000000
6.10000000000000
6.19999999999999
6.29999999999999
6.39999999999999
6.49999999999999
6.59999999999999
6.69999999999999
6.79999999999999

maybe this show the c++ better than f90 ?
:(
0 Kudos
david_jones
Beginner
1,160 Views
You have given a different number of decimal places in the two cases ... the results agree to the smaller number of places. For your test the most important value to quote will be the one at the end of the loop, where any inaccuracies will have had the chance to build up and you may or may not be on the right side of the upper bound due to numerical inaccuracy. It is the last value in the loop that is used in your test example code and the value of i may differ by 0.1

What are pgf90 etc.? As I recall, CVF precomputes how many times it will go round the loop by dividing the range by the step-size. Possibly C++ implements the loop by increasing i incrementally and testing each time whether it is within the limit.
0 Kudos
Intel_C_Intel
Employee
1,160 Views
Thank you much!
I think you are right,thank!

i don't know what you mean about 'what are pgf90',pgf90 is a very good f90 and cpp compiler on Linux.As i get the same result with pgi90,I think it just because of the fortran itself not of CVF.

http://www.pgroup.com/
0 Kudos
Reply