Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数学表达式在Google中工作,但在sqlite或KornShell中不起作用。_Sqlite_Shell_Math_Ksh_Arithmetic Expressions - Fatal编程技术网

数学表达式在Google中工作,但在sqlite或KornShell中不起作用。

数学表达式在Google中工作,但在sqlite或KornShell中不起作用。,sqlite,shell,math,ksh,arithmetic-expressions,Sqlite,Shell,Math,Ksh,Arithmetic Expressions,我观察到KornShell(ksh)或Sqlite中的数学表达式在与Google或Excel进行比较时并没有相同的结果 首先在Google中输入以下表达式: 8/(8+3)*9=6.545 现在请注意KornShell中的以下内容: > ksh > echo $(( 2 + 3 )) 5 > echo $((8 / (8 + 3) * 9)) 0 # Now change the first 8 to a 12 (higher than 11) and it works..

我观察到KornShell(ksh)或Sqlite中的数学表达式在与Google或Excel进行比较时并没有相同的结果

首先在Google中输入以下表达式: 8/(8+3)*9=6.545

现在请注意KornShell中的以下内容:

> ksh
> echo $(( 2 + 3 ))
5
>  echo $((8  / (8 + 3) * 9))
0
# Now change the first 8 to a 12 (higher than 11) and it works.... 
>  echo $((12  / (8 + 3) * 9)) 
9
现在请注意sqlite中的相同内容:

sqlite> select 8/(8 + 3) * 9;
8/(8 + 3) * 9
0

sqlite> select 12/(8 + 3) * 9;
12/(8 + 3) * 9
9

如何使这个表达式在KornShell和sqlite中都能工作?

sqlite有数据类型的概念。所有这些常数——12、8、3——都是整数常数。使用整数进行计算时,它不会将它们提升为浮点。Korn shell可能也会这样做。许多主流编程语言也是如此,尤其是C语言及其众多派生语言

如果要应用浮点规则,请将数字设为浮点:

select 12.0/(8.0 + 3.0) * 9.0

是的,在看到您的答案之前,确认这对ksh是正确的。祝大家好运。这就是像BC和DC这样的应用程序存在的原因。如果你想要更高的精度(保持在外壳级别),改变操作顺序(保持数学等价性)以获得最大的除法,并尝试获得最小的除法。因此,要解决这个问题,我需要将其转换为float或乘以1.0。参见:嗯,添加“0.0”似乎也修复了我的表达式:8/(8+3+0.0)*9=6.54,但8/(8+3)*9=0