There is a reason for preferring 1&| over (-<.) for getting the decimal part of a number: Did you know that (-<.) y can be negative for positive y?
] a=: 1-2^_44 1 ":!.18 a 0.99999999999994316 (-<.) a _5.68434e_14 - 2^_44 _5.68434e_14 1|a 0 The explanation is that the monad <. is tolerant, so that <.a is 1 even though 0<!.0 a, whence (-<.)a is a-1 which is - 2^_44 . 1&| is also tolerant, however the defn (as modeled in http://www.jsoftware.com/help/dictionary/d230.htm ) specifies that if (<.a)=>.a (which it is) then the result is 0 . p.s. 1&| will not be slower than (-<.) in every implementation of J. ----- Original Message ----- From: Rob Hodgkinson <[email protected]> Date: Sunday, May 3, 2009 3:13 Subject: Re: [Jprogramming] Getting the decimal values To: Programming forum <[email protected]> > Nothing, that would probably be my preferred choice too (Ric > pointed > this out subsequently). > > I was just focusing on the real question of print precision - > the same > print precision issue occurs for either approach. > > Note though that (- <.) performs faster albeit uses more space. > > dec1=:- <. > dec2=:1&| > 10{.z=:0.01*?100000$100000 > 318.72 415.11 155.08 826.2 664.99 381.41 148.83 758.18 562.25 117.54 > ts=: 6!:2 , 7!:2...@] > > 100 ts 'dec1 z' > 0.00573867950439 1573696 > 100 ts 'dec2 z' > 0.010124067688 1049472 > (dec1 -: dec2) z > 1 > > Thanks Oleg, Regards Rob > > On 03/05/2009, at 8:35 AM, Oleg Kobchenko wrote: > > > > > What's wrong with (1&|) ? > > > > > > ((- <.) -: 1&|) _5.1 _5.01 _0.0001 0 0.0001 5.1 5.2 5.0002 > > 1 > > > > > >> From: Rob Hodgkinson <[email protected]> > >> > >> Alex, you may be asking 'too much print precision' on your > display, > >> eg: > >> > >> decimal=:-<. > >> 9!:11 ]14 > >> > >> decimal 5.1 5.2 > >> 0.1 0.2 > >> 9!:11 ]15 > >> > >> decimal 5.1 5.2 > >> 0.0999999999999996 0.2 > >> 9!:11 ]16 > >> > >> decimal 5.1 5.2 > >> 0.09999999999999964 0.2000000000000002 > >> > >> You may need to use fmt (8!: x) to produce a specified format > to > >> avoid > >> the system rounding display. ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
