Re: question prompted by "Re: Tcl 8.5 sometimes produces numbers with long fractional parts"
- From: Robert Heller <heller@xxxxxxxxxxxx>
- Date: Sat, 21 Mar 2009 09:42:46 -0500
At Fri, 20 Mar 2009 15:21:11 -0500 Richard Owlett <rowlett@xxxxxxxxxxxxx> wrote:
Robert Heller wrote:
At Fri, 20 Mar 2009 11:35:17 -0700 (PDT) Arjen Markus <arjen.markus@xxxxxxxxxx> wrote:
Hello,
if you compute 0.2*3 in Tcl 8.5 the result is:
% expr {0.2*3}
0.6000000000000001
rather than the 0.6 I would have liked to get.
It is annoying in the context of Plotchart where sometimes you get
labels like this:
0.0, 0.2, 0.4, 0.6000000000000001, 0.8, 1.0
I can solve it via tcl_precision, but it remains a bit annoying. (I
guess there is no
easy numerical solution for this ...)
Regards,
Arjen
The above is *typical* of digital *binary* floating point arithmetic.
Don't ever expect that arithmetic involving nice 'rational' *decimal*
numbers like .2 or 3 to result in nice 'rational' *binary* numbers.
(.1, in binary is about as bad as 1/3 is as a decimal fraction).
Yes, you either need to set tcl_precision to some value that makes
sense OR use [format {%x.yf} $x] (with suitable values for x and y) to
generate the *textual* labels for your plots. I would most definately
recomend that for labels of a plot, you should always use the format
command, as this will insure that the labels are notionally consistent,
no matter what their values might be. And there are various 'tricks' to
generate the format string itself, based on the data range (hint: you
can use format itself to generate the format string).
Back in the "Good Old Days" when the 8085 was the latest thing.
Some microprocessors addressed this issue by having instructions which
did BCD arithmetic.
The BCD arithmetic ops in the 8085 was a holdover from the BCD
arithmetic ops in the 4004 and 4040. The *original* 'microprocessor'
(the 4004) was developed to be the heart of a line of multifunction
calculators, so it had instructions that were optimal for that
application. Some of the early 8-bit microprocessors also had some
similar instructions, for much the same purposes (and for things like
quick and easy implementation of simple-minded embeded functions -- eg
toaster ovens and the like).
Modern general purpose 32-bit and 64-bit processors don't bother with
these sorts of instructions. Between modern compilers and their
run-time-libraries (eg [sf]printf() and [sf]scanf()), etc.) there is
little advantage.
Does TCL support a similar solution?
I did a quick search for "BCD" and 'arbitrary precision' {which in TCL
doesn't seem to reference what I was thinking of} - found nothing.
I don't think it has any native "BCD" ALU instructions. Not that it
would not be possible to implement such, but it would not really save
any CPU cycles -- it would be faster to use normal *binary* integer and
floating point math and then use format to convert to fixed width
decimal string representation.
Generally, 'arbitrary precision' (aka bignums) is something else.
--
Robert Heller -- 978-544-6933
Deepwoods Software -- Download the Model Railroad System
http://www.deepsoft.com/ -- Binaries for Linux and MS-Windows
heller@xxxxxxxxxxxx -- http://www.deepsoft.com/ModelRailroadSystem/
.
- References:
- Tcl 8.5 sometimes produces numbers with long fractional parts
- From: Arjen Markus
- Re: Tcl 8.5 sometimes produces numbers with long fractional parts
- From: Robert Heller
- question prompted by "Re: Tcl 8.5 sometimes produces numbers with long fractional parts"
- From: Richard Owlett
- Tcl 8.5 sometimes produces numbers with long fractional parts
- Prev by Date: Re: Invoking Tcl procedures from c.
- Next by Date: Re: need a solution for this requirement..
- Previous by thread: Re: question prompted by "Re: Tcl 8.5 sometimes produces numbers with long fractional parts"
- Next by thread: Re: Tcl 8.5 sometimes produces numbers with long fractional parts
- Index(es):
Relevant Pages
|
Loading