1

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?

javaPlease42
  • 4,699
  • 7
  • 36
  • 65
user1888167
  • 131
  • 3
  • 9

1 Answers1

3

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
Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • 1
    yes, confirmed this is true for ksh (before seeing your answer). Good luck to all. – shellter Dec 11 '13 at 03:28
  • That's why application like BC and DC exist. If you whant the more precision (staying at shell level), change the order (keeping mathematical equivalence) of operation to have the biggest number divide and try to have the minimum of division occuring. – NeronLeVelu Dec 11 '13 at 07:15
  • So to fix this, it looks like I need to cast as float or multiply by 1.0. See: http://stackoverflow.com/questions/8305613/converting-int-to-real-in-sqlite – user1888167 Dec 11 '13 at 11:13
  • Hmm, Adding "0.0" also appears to fix my expression: 8/(8 + 3 + 0.0) * 9 = 6.54 BUT 8/(8 + 3) * 9 = 0 – user1888167 Dec 11 '13 at 11:18