On Wed, 24 Jul 2013 19:02:39 +0200 Tina Harriott wrote:
> Here's one of my little tough problems which I am unable to solve
> myself, even after looking at the source code of ast-ksh. The problem
> below requires a good understanding how floating point numbers are
> implemented in computers.

> I'm trying to prototype code and like to iterate over a small, linear
> area by using the C library function nextafter() to step forward the
> smallest possible distance between each step, and print the number of
> iterations needed to cover the distance between 4 and 4.000000000001:
> ksh -c 'float v ; integer iter ; for ((iter=0,v=4 ; v < 4.000000000001
> && iter < 10000000; v=nextafter(v,4.000000000001))) ; do
> ((iter++));done;print $iter '
> 2305843

> The result 2305843 is correct.

> However, if I use typeset -E (or just typeset) to declare the variable
> v the loop runs forever, or in this case until it hits iter < 10000000
> which I added as safeguard later:
> ksh -c 'typeset -E v ; integer iter ; for ((iter=0,v=4 ; v <
> 4.000000000001 && iter < 10000000; v=nextafter(v,4.000000000001))) ;
> do ((iter++));done;print $iter '
> 10000000

> Can anyone explain this?

float is an alias
this shows the alias definition
        type float
which is
        typeset -lE
this documents -l
        typeset --?l

so for your example
        typeset -E
gave you a double v and
        typeset -lE
would give you a "long double" v

_______________________________________________
ast-users mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-users

Reply via email to