Re: -0.0 [message #74533] |
Fri, 21 January 2011 16:13 |
kisCA
Messages: 78 Registered: January 2011
|
Member |
|
|
On Jan 20, 2:29 pm, Paolo <pgri...@gmail.com> wrote:
> On Jan 20, 2:22 pm, kisCA <ki...@hotmail.com> wrote:
>
>> I like this "sky is falling" things :-)
>
>> I guess that it could be a problem of precision with float...the range
>> should be higher in positive value in order to have 0 on the positive
>> side?
>
>> Thanks!
>
> Well yes. What happens is that when you do
>
> plot,[0,0],xrange=[-0.6,0.6],xtickv=vvv
>
> is that the plot range is given by
>
> r=!X.crange
>
> print,r,format='(f13.10)'
> -0.6000000238
> 0.5999999762
>
> IDL presumably uses the following formula to create 7 tick marks:
>
> tickv=r[0]+(r[1]-r[0])/6.0*findgen(7)
>
> print,tickv,format='(f13.10)'
> -0.6000000238
> -0.4000000238
> -0.2000000238
> -0.0000000238
> 0.1999999762
> 0.3999999762
> 0.5999999762
>
> When rounded to something more useful for plots:
>
> print,tickv,format='(f4.1)'
> -0.6
> -0.4
> -0.2
> -0.0
> 0.2
> 0.4
> 0.6
>
> That's how you get the negative zero.
>
> Ciao,
> Paolo
Well demonstrated. Thanks for the teaching!
Cheers
|
|
|
Re: -0.0 [message #74547 is a reply to message #74533] |
Thu, 20 January 2011 14:29  |
pgrigis
Messages: 436 Registered: September 2007
|
Senior Member |
|
|
On Jan 20, 2:22 pm, kisCA <ki...@hotmail.com> wrote:
> I like this "sky is falling" things :-)
>
> I guess that it could be a problem of precision with float...the range
> should be higher in positive value in order to have 0 on the positive
> side?
>
> Thanks!
Well yes. What happens is that when you do
plot,[0,0],xrange=[-0.6,0.6],xtickv=vvv
is that the plot range is given by
r=!X.crange
print,r,format='(f13.10)'
-0.6000000238
0.5999999762
IDL presumably uses the following formula to create 7 tick marks:
tickv=r[0]+(r[1]-r[0])/6.0*findgen(7)
print,tickv,format='(f13.10)'
-0.6000000238
-0.4000000238
-0.2000000238
-0.0000000238
0.1999999762
0.3999999762
0.5999999762
When rounded to something more useful for plots:
print,tickv,format='(f4.1)'
-0.6
-0.4
-0.2
-0.0
0.2
0.4
0.6
That's how you get the negative zero.
Ciao,
Paolo
|
|
|
Re: -0.0 [message #74552 is a reply to message #74547] |
Thu, 20 January 2011 11:26  |
kisCA
Messages: 78 Registered: January 2011
|
Member |
|
|
On Jan 20, 11:22 am, kisCA <ki...@hotmail.com> wrote:
> Thanks!
Both of you!
|
|
|
Re: -0.0 [message #74553 is a reply to message #74552] |
Thu, 20 January 2011 11:22  |
kisCA
Messages: 78 Registered: January 2011
|
Member |
|
|
I like this "sky is falling" things :-)
I guess that it could be a problem of precision with float...the range
should be higher in positive value in order to have 0 on the positive
side?
Thanks!
|
|
|
Re: -0.0 [message #74555 is a reply to message #74553] |
Thu, 20 January 2011 11:16  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Paolo writes:
> This falls into the "sky is falling" category, although with
> a slight different twist.
>
> http://www.dfanning.com/math_tips/sky_is_falling.html
>
> Signed zeros are allowed in the IEEE 754 standard for floating point
> arithmetic.
>
> IDL> print,-2.0*0
> -0.00000
> IDL> print,2.0*0
> 0.00000
>
> If you don't like that in the plot (and I agree that it looks ugly),
> you should
> manually change that tick label using the xtickname keyword.
>
> However, that is painful to do, so you could try the following hack:
>
> xrange=[-0.6,0.6]
> plot,[0,0],/nodata,/xstyle,xrange=xrange,title='this looks bad'
>
> ;workaround
> epsilon=1e-6
> xrange=[-0.6,0.6]
> xrange=xrange+epsilon*[-1,2]
> plot,[0,0],/nodata,/xstyle,xrange=xrange,title='better now'
>
>
> You see what happened there? Care to guess why it worked?
Here is another solution.
FUNCTION zeroAxis, axis, index, value
absvalue = Abs(value)
PRINT, value, absvalue, Format='(2(F0.8,2x))'
IF absvalue LT 0.001 THEN value = 0.0
IF value LT 0 THEN BEGIN
strValue = '-' + String(absvalue, Format='(F0.1)')
ENDIF ELSE BEGIN
strValue = String(absvalue, Format='(F0.1)')
ENDELSE
RETURN, strValue
END
x = -.6+indgen(13)*.1
plot, x, findgen(11), /nodata, $
xstyle=1, xtickformat='ZeroAxis'
END
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
Re: -0.0 [message #74556 is a reply to message #74555] |
Thu, 20 January 2011 11:11  |
pgrigis
Messages: 436 Registered: September 2007
|
Senior Member |
|
|
On Jan 20, 1:40 pm, kisCA <ki...@hotmail.com> wrote:
> Hi there,
>
> I am trying to make a plot with xaxis from -0.6 to 0.6. On the xaxis
> it gaves me a -0.0 ? I tried xtickformat='(F4.1)' and '(F0.1)' but no
> success...
>
> pcit= -.6+indgen(13)*.1
>
> !p.font=0 ;use postscript fonts
> set_plot, 'ps'
> ext='.eps'
> cs=0 ;charcter size
> !p.thick=3 ;data
> !x.thick=2 ;x axis
> !y.thick=2 ;y axis
> device, filename='Response Solar Cycle - Annual Mean'+ext,
> encapsulated=eps, $
> /helvetica,/isolatin1, xsize=8, ysize=12,font_size=8, landscape=0,
> decomposed=0, color=1
> plot,pcit,Zproxy,/nodata,ystyle=1,yrange=[15,50],xtickformat ='(F4.1)'
> oplot,Solarcoef*100,Zproxy,color=0,linestyle=0
> oplot,(Solarcoef+Solarstd)*100,Zproxy,color=204,linestyle=1
> oplot,(Solarcoef-Solarstd)*100,Zproxy,color=204,linestyle=1
> oplot,limiteY,Zproxy,color=0,linestyle=2
> for z=0,n_elements(Zproxy)-1 do begin
> oplot,[(Solarcoef(z)-Solarvar(z))*100,(Solarcoef(z)
> +Solarvar(z))*100],[Zproxy(z),Zproxy(z)],color=226,linestyle =0
> endfor
> oplot,[-.500,-.400],[48,48],color=0,linestyle=0
> xyouts,-.350,48,'Mean'
> oplot,[-.500,-.400],[47,47],color=204,linestyle=1
> xyouts,-.350,47,'Std'
> oplot,[-.500,-.400],[46,46],color=226,linestyle=0
> xyouts,-.350,46,'Error'
> device,/close
>
> Do you have an idea ?
>
> Cheers
This falls into the "sky is falling" category, although with
a slight different twist.
http://www.dfanning.com/math_tips/sky_is_falling.html
Signed zeros are allowed in the IEEE 754 standard for floating point
arithmetic.
IDL> print,-2.0*0
-0.00000
IDL> print,2.0*0
0.00000
If you don't like that in the plot (and I agree that it looks ugly),
you should
manually change that tick label using the xtickname keyword.
However, that is painful to do, so you could try the following hack:
xrange=[-0.6,0.6]
plot,[0,0],/nodata,/xstyle,xrange=xrange,title='this looks bad'
;workaround
epsilon=1e-6
xrange=[-0.6,0.6]
xrange=xrange+epsilon*[-1,2]
plot,[0,0],/nodata,/xstyle,xrange=xrange,title='better now'
You see what happened there? Care to guess why it worked?
Ciao,
Paolo
|
|
|