arithmetic and logic

スクリプト言語の比較

http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。

arithmetic and logic
























































































  perl php python ruby
true and false

1 0

<?php
TRUE FALSE # case insensitve
?>

True False

true false

falsehoods

undef 0 0.0 '' '0' ()

<?php
FALSE NULL 0 0.0 '' '0' array()
?>

False None 0 0.0 '' [] {}

false nil

logical operators

and or not also: && || !

<?php
&& || ! lower precedence: and or xor
?>

and or not

and or not also: && || !

conditional expression

$x > 0 ? $x : -$x

<?php
$x > 0 ? $x : -$x
?>

x if x > 0 else -x

x > 0 ? x : -x

comparison operators

numbers only: == != > < >= <=
strings: eq ne gt lt ge le

<?php
== != or <> > < >= <=
no conversion: === !==
?>

== != > < >= <=

== != > < >= <=

convert from string, to string

7 + '12'
73.9 + '.037'
'value: ' . 8

<?php
7 + '12'
73.9 + '.037'
'value: ' . 8
?>

7 + int('12')
73.9 + float('.037')
'value: ' + str(8)

7 + "12".to_i
73.9 + ".037".to_f
"value: " + "8".to_s

arithmetic operators

+ - * / none % **

<?php
+ - * / none % pow(b,e)
?>

+ - * / // % **

+ - * x.fdiv(y) / % **

integer division

int ( $a / $b )

<?php
(int) ($a / $b)
?>

a // b

a / b

float division

$a / $b

<?php
$a / $b
?>

float(a) / b
# python 3:
a / b

a.to_f / b or
a.fdiv(b)

arithmetic functions

sqrt
exp
log
sin
cos
none
none
none
none
atan2

<?php
sqrt
exp
log
sin
cos
tan
asin
acos
atan
atan2
?>

from math import sqrt, exp, log, \
sin
cos
tan
asin
acos
atan
atan2

include Math
sqrt
exp
log
sin
cos
tan
asin
acos
atan
atan2

arithmetic truncation

abs($x)
none
POSIX:
ceil($x)
floor($x)

<?php
abs($x)
round($x)
ceil($x)
floor($x)
?>

import math
abs(x)
int(round(x))
math.ceil(x)
math.floor(x)

x.abs
x.round
x.ceil
x.floor

min and max

use List::Util qw( min max);
min(1,2,3);
max(1,2,3);
@a = (1,2,3);
min(@a);
max(@a);

<?php
min(1,2,3)
max(1,2,3)
# of an array:
$a = array(1,2,3)
call_user_func_array(min, $a)
call_user_func_array(max, $a)
?>

min(1,2,3)
max(1,2,3)
min([1,2,3])
max([1,2,3])

[1,2,3].min
[1,2,3].max

division by zero

error


returns zero with warning


raises ZeroDivisionError


integer division
raises ZeroDivisionError
float division returns Infinity
integer overflow

converted to float


converted to float


becomes arbitrary length
integer of type long


becomes arbitrary length
integer of type Bignum
float overflow

inf

<?php
INF
?>



raises OverflowError

Infinity

sqrt -2

error

<?php
NaN
?>

# raises ValueError:
import math
math.sqrt(-2)
# returns complex float:
import cmath
cmath.sqrt(-2)



raises Errno::EDOM
rational numbers

none


none

from fractions import Fraction
x = Fraction(22,7)
x.numerator
x.denominator

require 'rational'
x = Rational(22,7)
x.numerator
x.denominator

complex numbers

none


none

z = 1 + 1.414j
z.real
z.imag

require 'complex'
z = 1 + 1.414.im
z.real
z.imag

random integer, uniform float, normal float

int(rand() * 100)
rand()
none

<?php
rand(0,99)
lcg_value()
none
?>

import random
random.randint(0,99)
random.random()
random.gauss(0,1)

rand(100)
rand
none

bit operators

<< >> & | ^ ~

<?php
<< >> & | ^ ~
?>

<< >> & | ^ ~

<< >> & | ^ ~