This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Linux / UNIX and the fly likes Trig functions in the Korn Shell? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Trig functions in the Korn Shell?" Watch "Trig functions in the Korn Shell?" New topic
Forums: Mac OS Linux / UNIX
Author

Trig functions in the Korn Shell?

Simon Ingram
Ranch Hand

Joined: Nov 30, 2003
Posts: 173
Hi folks,

I have read that the Korn Shell has built in Trig functions like sine and cosine. Imagine my disappointment when I get an unknown function message.
I am running on a MAC with OS X. It has a Korn shell, but I don't know if its ksh 88 or ksh 93. There should be a way of interrogating the system for this info, but I don’t know how.
I tried uname –a but it didn’t help. I would be very surprised if I am using the old Korn which raises the question: why don’t the trig functions work?

Here’s my test script:

#!/bin/ksh
##
val=$( bc -l <<EOF
a(1) # arctan 1 is 45 degrees
EOF)
##
echo $val
#
## shell trig functions
sin45=$(( sin($val) ))
echo $sin45

When I run it:

$ ksh simon_bc
.78539816339744830961
simon_bc[10]: sin(.78539816339744830961) : unknown function

AM I missing something?

With thanks and Best Regards
Simon>
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42367
    
  64
I don't think the version of the shell (Korn or otherwise) has anything to do with that - you're invoking the "bc" executable that is part of all Unix-like OSs: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html.

The sine function is called "s", not "sin".


Ping & DNS - my free Android networking tools app
Simon Ingram
Ranch Hand

Joined: Nov 30, 2003
Posts: 173
Hi Ulf,

thanks for the reply. You are partially correct, if I had tried to execute the sine function within bc then it would be "s" not "sin". But in the script the bracket after EOF (line 5) terminates the bc command and everything following is executed in the shell. The sine command in the korn shell is "sin" not "s".

In fact we can ignore the bc altogether and just try to execute the sin command in the shell hardcoding the argument. I can't get it to work! Arithmetic syntax error.

Best Regards
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42367
    
  64
No idea about the shell. Why don't you use bc for the entire calculation? It would seem to be better suited than any shell.
Simon Ingram
Ranch Hand

Joined: Nov 30, 2003
Posts: 173
You are right, there are many ways to get the job done, but I am interested in the Korn shell and what it can do. I have a book on the shell, that tells me it can do trig functions and when I put in an example from the book, I get an error. It's kind of exasperating! I just wonder what I'm doing wrong.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42367
    
  64
A quick googling shows that those functions apparently need to be in double parentheses, but not a $ sign: https://suite.io/mark-alexander-bain/zfj2aa
This works fine on OS X:

Simon Ingram
Ranch Hand

Joined: Nov 30, 2003
Posts: 173
Hi Ulf,

many thanks for your persistence. Here is my program: trig_test.ksh

#!/bin/ksh
(( d = sin(1.0) ))
echo $d

/Users/simoningram/unix/bin => trig_test.ksh
/Users/simoningram/unix/bin/trig_test.ksh: line 3: d = sin(1.0) : arithmetic syntax error

I too have used google and tried all the different syntax variations, but this problem is more fundamental, as I suggested in my first post.

Best Regards
Simon

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42367
    
  64
Hm, strange. I actually stand corrected in that within a script, the $ is needed. But is still works fine for me on Mavericks:


Simon Ingram
Ranch Hand

Joined: Nov 30, 2003
Posts: 173
Hi Ulf,

I haven't installed Mavericks. Maybe I should do that?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42367
    
  64
I have never used the Korn shell before, so I don't know what difference the OS X version
makes.
Simon Ingram
Ranch Hand

Joined: Nov 30, 2003
Posts: 173
Mavericks didn't make any difference.
The strange thing is that I ran this script on my Mac at home and on our unix system at work and in both cases it fails. I will investigate this at work with our specialists, but it is very good to know that you have managed to get the script working on your system, so thanks Ulf.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11490
    
  95

Works fine for me as well, using Mountain Lion and ksh version 93u 2011-02-08


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
 
 
subject: Trig functions in the Korn Shell?