歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux服務器 >> Linux/Unix下常用數學計算

Linux/Unix下常用數學計算

日期:2017/3/2 16:40:27   编辑:Linux服務器

有時候我們需要做一些簡單的計算,

如:1.1^10 應該是多少,計算一些百分率等等小事情,如果在windows下我們就知道回去用計算器了,Linux/Unix下怎麼辦呢。

會寫C當然很快就可以解決了。但有點大才小用了,以前我一直使用gdb下的print來做,但有時候還是不大好,如: 1.1^10這樣的計算目前還不知道怎麼解決,簡單了了解了一下,原來shell中的萬能的awk就可以幫我們了。

看一些簡單的事例:

[oracle@asm tmp]$ gdb

GNU gdb Red Hat Linux (6.3.0.0-1.63rh)

Copyright 2004 Free Software Foundation, Inc.

GDB is free software, covered by the GNU General Public License, and you are

welcome to change it and/or distribute copies of it under certain conditions.

Type "show copying" to see the conditions.

There is absolutely no warranty for GDB. Type "show warranty" for details.

This GDB was configured as "i386-redhat-linux-gnu".

(gdb) p 1.4*123.324*3

{GetProperty(Content)} = 517.96079999999995

(gdb) p /x 1.4*123.324*3

$2 = 0x205

(gdb) p /x 4*24

$3 = 0x60

(gdb)

[oracle@asm tmp]$ echo | awk '{print 1.1*2*23.34;}'

51.348

[oracle@asm tmp]$ awk 'BEGIN {print (1.25^10);}'

9.31323

awk 支持不少常見的運算符, 如:

+ (加),- (減), * (乘), / (除), ^ 或 **(乘方), % 取模) 等等。

此外, awk 也提供了一些常用的數學函數, 比如 sin(x), cos(x), exp(x), log(x), sqrt(x), rand()。 使用這些運算符和函數可以直接進行一些簡單的運算:

[oracle@asm tmp]$ echo | awk '{print sin(2);}'

0.909297

如果比會使用的話這個其實比使用windows下的計算器,C寫的小程序要的來的快多了

Copyright © Linux教程網 All Rights Reserved