On Tue, 28 Nov 2017 09:52:26 +0000, Otto Moerbeek wrote: > On Sun, Nov 26, 2017 at 07:51:13PM +0000, kshe wrote: > > Hi, > > > > The following behaviour seems unacceptable to me. > > > > $ dc -e '16dio .C .C0 f' > > .C0 > > .B > > > > $ dc -e '8dio .3 .30 .300 f' > > .3000 > > .275 > > .23 > > > > This bug affects all bases other than 10 and increasing the scale with > > the `k' command does not prevent it. The only reliable way to input a > > rational number in these bases is therefore to input an integer first, > > and then divide it by the appropriate factor; alternatively, as > > illustrated above, adding meaningless zeros to the digit expansion can > > help, although I have not verified that this works in all cases nor how > > many zeros would need to be added to guarantee the expected result > > every time. > > > > I could try to provide a patch for this, but I dislike how the current > > code gives special privileges to base 10 at the expense of all the > > others, even though it is supposed to accept input in any base; so my > > patch would probably be deemed too invasive as I would want to change > > too many things at once, including in areas irrelevant to this > > particular issue. > > > > If a less intrusive workaround is not possible, then perhaps at least a > > warning about fractional input being essentially broken in bases other > > than 10 could be added to the manual. > > > > Regards, > > > > kshe > > I implemented dc(1) to stay as close as possible to the original dc(1) > and GNU dc(1). Any change in the way fractional numbers are handled > (even though you might consider it broken) will break existing > programs.
I agree, and this is why I refrained from submitting a patch for this one. That said, since `.C' being effectively interpreted as `.B' is probably not what any new user would expect, could something like the following be useful? (This diff also includes an unrelated spelling fix.) Index: dc.1 =================================================================== RCS file: /cvs/src/usr.bin/dc/dc.1,v retrieving revision 1.31 diff -u -p -r1.31 dc.1 --- dc.1 28 Nov 2017 06:51:19 -0000 1.31 +++ dc.1 30 Nov 2017 04:00:42 -0000 @@ -245,7 +245,7 @@ Mark used by the operator. The .Ic M -operator is a non-portable extensions, used by the +operator is a non-portable extension, used by the .Xr bc 1 command. .It Ic N @@ -536,3 +536,8 @@ The current version of the .Nm utility was written by .An Otto Moerbeek . +.Sh CAVEATS +While fractional input in base 10 is always exact, +other bases may suffer from unintuitive rounding. +To avoid surprising results, plain integer division can be used +instead of the corresponding floating point notation. Regards, kshe
