bug-sh-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bug in seq


From: Bob Proulx
Subject: Re: bug in seq
Date: Wed, 21 Nov 2001 00:10:06 -0700

> I get the following outputs from seq, and they are obviosly contraversary:
> 
> $ seq -s ' ' .9 .03 1.5
> 0.9 0.93 0.96 0.99 1.02 1.05 1.08 1.11 1.14 1.17 1.2 1.23 1.26 1.29 1.32
> 1.35 1.38 1.41 1.44 1.47 1.5
> 
> $ seq -s ' ' .9 .03 1.2
> 0.9 0.93 0.96 0.99 1.02 1.05 1.08 1.11 1.14 1.17
> 
> Why isn't 1.2 written in the second run, or why 1.5 is written in the
> first?

The seq program uses floating point values.  Floating point numbers
are only an approximation.  The values are not exact and some epsilon
of imprecision is frequently seen when working with fractional
amounts.  Although humans usually work in decimal format the internal
representation is binary.  Some decimal numbers cannot be represented
as an exact binary number.

What you are seeing is a small numerical imprecision when dealing with
floating point numbers.  The repeated increment of 0.9 by 0.03 is
producing a final value greater than 1.2 by a very small amount and
therefore seq is terminating the sequence without printing it.

This can be worked around by adding a small amount to the end point
which is smaller than the increment.

  seq -s ' ' .9 .03 1.21
  0.9 0.93 0.96 0.99 1.02 1.05 1.08 1.11 1.14 1.17 1.2

Bob



reply via email to

[Prev in Thread] Current Thread [Next in Thread]