Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. #!/usr/bin/env Rscript
  2.  
  3. sys = modules::import('sys')
  4.  
  5. "A simple mathematical expression evaluator"
  6. VERSION = 1.0
  7.  
  8. sys$run({
  9. args = sys$cmdline$parse(opt('n', 'allow-nan', 'allow NaN results (otherwise, the program exits with a failure)', FALSE),
  10. opt('d', 'digits', 'number of digits to print', 0),
  11. arg('expression', 'mathematical expression'))
  12.  
  13. result = eval(parse(text = args$expression))
  14. if (! args$allow_nan && is.nan(result))
  15. sys$exit(1, 'NaN result invalid')
  16. sys$printf('%.*f', args$digits, result)
  17. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement