Question

I observed that math expressions in KornShell (ksh) or Sqlite don't have equivalent outcomes when comparing them to Google or Excel.

First put the expression below in Google: 8 / (8 + 3) * 9 = 6.54545454545

Now note the following in 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

Now note the same in sqlite:

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

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

How can I make this expression work in both KornShell and sqlite?

Était-ce utile?

La solution

sqlite has the notion of a datatype. All those constants - 12, 8, 3 - are integer constants. When making calculations with integers, it does not promote them to floating-point. Korn shell probably does the same. So would many mainstream programming languages, notably C and its numerous derivatives.

If you want floating point rules to apply, make your numbers floating point:

select 12.0/(8.0 + 3.0) * 9.0
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top