Problem with the command Make
Zolis
I wrote a really small function to create 2D waves which has a strange problem:
Here is the code
function make_measwaves(Pstart,Pstop,Pstep) ////// this function creates g1 and g1_2D
Variable Pstart,Pstop,Pstep
Variable Pnumpts =abs((Pstart-Pstop))/Pstep
print "Pnumpts="+num2str(Pnumpts)
make/O/N=(Pnumpts) g1=NaN
make/O/N=(190,Pnumpts) g1_2D=NaN
make/O/N=200 gx=NaN
make/O/N=(190,200) gx_2D=NaN
end
If I execute make_measwaves(400,200,1) their is no problem
BUT if I execute make_measwaves(9.59,9.79,0.001) g1 will have only 199 points and g1_2D will have only 199 column even if Pnumpts = 200
Any ideas?
Thanks
It looks like that
Make
is truncating the numbers given for the dimension. And not rounding as you might expect.Try the following code:
Variable Pstart,Pstop,Pstep
Variable Pnumpts = abs((Pstart-Pstop))/Pstep
printf "Pnumpts = %.15g\r", Pnumpts
make/O/N=(Pnumpts) g1=NaN
make/O/N=(190,Pnumpts) g1_2D=NaN
make/O/N=(round(Pnumpts)) g1_new=NaN
end
Thomas
PS: I you use the igor tags the pasted code will be much better readable.
January 8, 2016 at 04:42 am - Permalink
0.199999999999999
See this for a description of the problem: https://en.wikipedia.org/wiki/Floating_point
We often get bug reports about problems that result from floating-point representation. Note that 0.1 cannot be exactly represented as a base-2 number.
The solution is judicious use of round(), ceil() and floor().
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
January 8, 2016 at 11:33 am - Permalink
thanks John and Tomas for the inputs, I get the problem now.
I'll try to use round, ceil or floor to solve my issue.
January 11, 2016 at 01:13 am - Permalink