From: Amit Zaroo on 2 Feb 2010 03:41 Hi, I wanted to know the difference if any between using the following expressions. I am getting different results. The manual says any valid numeric operands are supported. It seems only positive operands are supported. $ tclsh % set exp -7 -7 % set result [expr 2**$exp] 0 % set result [expr 2**(-1*$exp)] 128 % set result [expr pow(2,$exp)] 0.0078125 % set result [expr pow(2,-$exp)] 128.0
From: Arjen Markus on 2 Feb 2010 03:51 On 2 feb, 09:41, Amit Zaroo <amit.za...(a)gmail.com> wrote: > Hi, > I wanted to know the difference if any between using the following > expressions. I am getting different results. The manual says any valid > numeric operands are supported. It seems only positive operands are > supported. > > $ tclsh > % set exp -7 > -7 > % set result [expr 2**$exp] > 0 > % set result [expr 2**(-1*$exp)] > 128 > % set result [expr pow(2,$exp)] > 0.0078125 > % set result [expr pow(2,-$exp)] > 128.0 The essential difference between ** and pow() is that pow() treats its arguments as double precision reals, so pow(2,-7) is equivalent to 2.0**(-7.0) and the ** operation treats its arguments according to the values, i.e. integers are distinguished from reals, both in the exponent and the base number. Hence the differences! Regards, Arjen
From: Amit Zaroo on 2 Feb 2010 09:58 Thanks for the explanation. It is now clear! Regards, Amit Zaroo Arjen Markus wrote: > On 2 feb, 09:41, Amit Zaroo <amit.za...(a)gmail.com> wrote: > > Hi, > > I wanted to know the difference if any between using the following > > expressions. I am getting different results. The manual says any valid > > numeric operands are supported. It seems only positive operands are > > supported. > > > > $ tclsh > > % set exp -7 > > -7 > > % set result [expr 2**$exp] > > 0 > > % set result [expr 2**(-1*$exp)] > > 128 > > % set result [expr pow(2,$exp)] > > 0.0078125 > > % set result [expr pow(2,-$exp)] > > 128.0 > > The essential difference between ** and pow() is that pow() treats its > arguments as double precision reals, so pow(2,-7) is equivalent to > 2.0**(-7.0) > and the ** operation treats its arguments according to the values, > i.e. > integers are distinguished from reals, both in the exponent and the > base number. > > Hence the differences! > > Regards, > > Arjen
|
Pages: 1 Prev: One more idiot "button -command" question Next: tk scrollbar command |