Common mathematical computing in Linux/Unix

Source: Internet
Author: User

Sometimes we need to do some simple computing,

For example, how much should 1.1 ^ 10 be, and how to calculate the percentage? in windows, we will know how to use a calculator. What should we do in Linux/Unix.

Of course, writing C will soon solve the problem. I used to use print under gdb, but sometimes it is still not good, such as 1.1 ^ 10, after a brief introduction, the omnipotent awk in shell can help us.

Let's look at some simple examples:

[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

$1 = 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 'in in {print (1.25 ^ 10 );}'

9.31323

Awk supports many common operators, such:

+ Plus),-minus), * multiplication),/division), ^ Or ** multiplication Party), % modulo), and so on.

In addition, awk provides some common mathematical functions, such as sin (x), cos (x), exp (x), log (x), sqrt (x ), rand (). Using these operators and functions, you can directly perform some simple operations:

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

0.909297

If you want to use it, it is actually much faster than using a calculator and C program in windows.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.