• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trig functions in the Korn Shell?

 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Simon Ingram
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ulf,

I haven't installed Mavericks. Maybe I should do that?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have never used the Korn shell before, so I don't know what difference the OS X version
makes.
 
Simon Ingram
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Works fine for me as well, using Mountain Lion and ksh version 93u 2011-02-08
 
reply
    Bookmark Topic Watch Topic
  • New Topic