Skip to content

Instantly share code, notes, and snippets.

@helgee
Created July 1, 2014 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helgee/f179b9610aa104e38966 to your computer and use it in GitHub Desktop.
Save helgee/f179b9610aa104e38966 to your computer and use it in GitHub Desktop.
Floating Point Error
#include <stdio.h>
#include <math.h>
double f(double t, double y) {
return y*cos(t);
}
int main() {
double yjulia = 2.31111366572489;
double yfortr = 2.3111136657248901;
double t = 0.7012954297850128;
double c7 = 0.25;
double h = 1.1672276724655861;
printf("%20.16f\n", f(t+c7*h, yfortr));
printf("%20.16f\n", f(t+c7*h, yjulia));
}
program test
implicit none
double precision :: yjulia
double precision :: yfortran
double precision :: t
double precision :: c7
double precision :: h
yjulia = 2.31111366572489d0
yfortr = 2.3111136657248901d0
t = 0.7012954297850128d0
c7 = 0.25d0
h = 1.1672276724655861d0
print *, f(t+c7*h, yfortr)
print *, f(t+c7*h, yjulia)
contains
double precision function f(t,y)
double precision, intent(in) :: t
double precision, intent(in) :: y
f = y*cos(t)
end function
end program
#! /usr/bin/env julia -F
f(t,y) = y*cos(t)
t = 0.7012954297850128
yjulia = 2.31111366572489
yfortr = 2.3111136657248901
c7 = 0.25
h = 1.1672276724655861
println(f(t+c7*h, yfortr))
println(f(t+c7*h, yjulia))
#! /usr/bin/env python
from __future__ import print_function
import math
def f(t,y):
return y*math.cos(t)
t = 0.7012954297850128
yjulia = 2.31111366572489
yfortr = 2.3111136657248901
c7 = 0.25
h = 1.1672276724655861
print(repr(f(t+c7*h, yfortr)))
print(repr(f(t+c7*h, yjulia)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment