Mathematics in IronPython

Perşembe, 11 Mart 2010 14:19 by ikivanc
In this article we will see Numeric Funtions and calculations in IronPython

Here is 4 basic mathematical operators in IronPython ;

Addition         +
Substraction    -
Division          /
Multiplication   *


>>> 2+2     #we can use this expressions same as math in daily life
 4
 
 >>> 5-6
 -1
 
 >>> 4-3
 1
 
 >>> 52*9
 468
 
 >>> 12*-1
 -12
 
 >>> 4/3  
 1                  #After division result returns as an integer
 
 >>> x=2.1
 >>> y=3.1
 >>> z=x/y
 >>> z=float(z)     #if we set z as a float, result returns as a float.
 >>> print z
 0.677419354839
 >>> z=int(z)    #if we set z as an integer, result returns as a integer.
 >>> print z
 0
 
 >>> x=3
 >>> y=8
 >>> x=str(x)   #Let's see what happens when add converted intergers as strings
 >>> y=str(y)
 print x+y
 38 

Bacause of no pre-declaration, division restult retuns defaultly as an integer. We can convert this result to other types.

Power of a number
This method retuns x to the power of y the value of xy.
 
 4^3 = >>> 4**3
 64 
 
 4^(2^6) = 4**2**6 
 >>> 4**2**2
 256 
 

Complex Numbers

This gives us the form of x + yj ,real and imaginery numbers. "j" represents the square of (-1)
 
 >>> (4+4j)+(2-5j)
 (6-1j) 
 >>> (4+4j)/(2-5j)
 (-0.41379310344827586+0.96551724137931039j)
 
 >>> (4+4j)*(2-5j)
 (28-12j) 

We can use 4 basic mathematical operations with complex numbers.

Other Common Numeric Funtions
 >>> import math        # we imported Math library
 >>> math.pi           # we call PI in Math library
 3.14159265359 

Now we can use math library of python or math library of .NET with IronPython.


Abs(), Pow() Functions
Abs function returns absolute value of negative numbers. Pow function returns the value x to the power of y (xy)

 >>> abs(-42), 2**4, pow(2, 4)
 (42, 16, 16) 

PDF version of this article >> 9 - Mathematics in IronPython

If you have any questions or discover any errors / typos please let me know ik@ibrahimkivanc.com
 
All The Best! 

Yorum ekle


 

  Country flag

biuquote
  • Yorum
  • Canlı önizleme
Loading