BC及其ibase/obase选项: [英] bc and its ibase/obase options:

查看:426
本文介绍了BC及其ibase/obase选项:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了一个奇怪的错误:

I stumbled over a curious bug, I think:

我试图将"512"读取为以6为底的数字,并将其输出为以16为底的数字

I tried to read "512" as a number to base 6, and output it as base 16:

echo "ibase=6;obase=16;512" | bc
161

如您所见,输出为161,但应为bc(原文如此!).我尝试以10为基数:

As you can see, the output is 161, but it should be bc(sic!). I tried with base 10:

echo "ibase=6;obase=10;512" | bc
512

该值未更改.好奇的!默认观测值是10.如果我忽略它:

The value is unchanged. Curious! Default obase is 10. If I omit it:

echo "ibase=6;512" | bc
188

好吧,这似乎是对的.在两步过程中,它可以正常工作:

Well, that seems right. In a two step process, it works:

echo "obase=16;"$(echo "ibase=6;512" | bc) | bc
BC

所以我为不同的基础制作了一个脚本,但这让我感到困惑:

So I made a script for different bases, but it keeps me puzzled:

for ib in {6,8,10,16}; do echo $ib; for ob in {10,16}; do echo -en $ib $ob"     \t => " ; echo "ibase=$ib;obase=$ob;333" | bc ; done; done; 
6
6 10         => 333
6 16         => 108
8
8 10         => 333
8 16         => 119
10
10 10        => 333
10 16        => 14D
16
16 10        => 333
16 16        =>  01 15 05

我应该提交错误报告,还是错过明显的内容?我真的不能相信这样的基本工具已经坏了.

Shall I file a bugreport, or do I miss the obvious? I can't really believe that such a basic tool is broken.

推荐答案

不是错误.

一旦解释了ibase=6,就以6为底读取了数字.因此ibase=6;obase=16使得obase的值为16 以6为底的,该值无效,并被解释为11 十进制.

As soon as ibase=6 is interpreted, numbers are read in base 6. So ibase=6;obase=16 makes obase's value to be 16base 6 which is invalid, and is interpreted as 11decimal.

在手册页中:

对于多位数字,bc将所有大于或等于ibase的输入数字更改为ibase-1的值.

For multi-digit numbers, bc changes all input digits greater or equal to ibase to the value of ibase-1.

因此16被解释为15 base 6 ,即11 十进制.并且转换是正确的.

So 16 is interpreted as 15base 6 which is 11decimal. And the conversion is correct.

ibase之前设置obase,或确保在基准ibase中指定您的观测.

Set obase before ibase, or make sure to specify your obase in base ibase.

$ echo "obase=16;ibase=6;512" | bc
BC

这篇关于BC及其ibase/obase选项:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆