machine precision [message #66507] |
Mon, 18 May 2009 04:41 |
Wout De Nolf
Messages: 194 Registered: October 2008
|
Senior Member |
|
|
Hi all,
When checking whether two foating point variables are equal, one has
to do this:
pres = (machar()).eps
bequal = abs(f1-f2) lt pres
This can go wrong however, as illustrated by the example below. Do I
need to do error propagation on this? This means that every time f1
and f2 are calculated differently, I have to calculate a different
uncertainty? This seems like a lot of work, not to mention the machine
precision in calculation the propagation of uncertainty... Is there a
more general rule of thumb I can use?
vec1=[1.,2,3,4,5]
vec2=vec1
pres=(machar()).eps
norm1=sqrt(total(vec1^2,1,/pres))
norm2=sqrt(total(vec2^2,1,/pres))
f1=total(vec1*vec2,/pres) ; inner product
f2=norm1*norm2 ; product of the norms
; f1 and f2 must be equal so
if abs(f1-f2) ge pres then print,'wrong wrong wrong...'
|
|
|