$[5/2] (нираспарсил)
Модераторы: /dev/random, Модераторы разделов
-
drBatty
- Сообщения: 8735
- Статус: GPG ID: 4DFBD1D6 дом горит, козёл не видит...
- ОС: Slackware-current
$[5/2]
на http://root.yandex.ru/var/log/game.log.2 наткнулся на сабж. В принципе понятно, что ответ два. И что $[111/2] даст 55. Но почему-то не нашёл этого в man bash. Ткните носом пожалуйста...
-
watashiwa_daredeska
- Бывший модератор
- Сообщения: 4038
- Статус: Искусственный интеллект (pre-alpha)
- ОС: Debian GNU/Linux
Re: $[5/2]
ARITHMETIC EVALUATION
Evaluation is done in fixed-width integers with no check for overflow,…
The operators and their precedence, associativity, and values are the same as in the C language.
Мои розовые очки
-
drBatty
- Сообщения: 8735
- Статус: GPG ID: 4DFBD1D6 дом горит, козёл не видит...
- ОС: Slackware-current
Re: $[5/2]
watashiwa_darede...
спасибо, но я думал, что это про (( arithmetic expressions ))
я не знал, что $[ arithmetic expressions ] тоже самое (?)
спасибо, но я думал, что это про (( arithmetic expressions ))
я не знал, что $[ arithmetic expressions ] тоже самое (?)
-
eddy
- Сообщения: 3321
- Статус: Красный глаз тролля
- ОС: ArchLinux
Re: $[5/2]
Кстати, можно еще в .bashrc запихать вот это
И выполнять простенькие вычисления в баше:
Вот только с простыми арифметическими операциями проблемка - без bc уже не обойтись.
Код: Выделить всё
function round(){ [ "$2" = "" ] && NUM=3 || NUM=$2; echo $1 | sed "s/\([[:digit:]]*\.[[:digit:]]\{,$NUM\}\).*/\1/"; }
function sin(){ echo "scale=10; s($1*a(1)/45);" | bc -l; }
function cos(){ echo "scale=10; c($1*a(1)/45);" | bc -l; }
function tan(){ echo "scale=10; arg=$1*a(1)/45; s(arg)/c(arg);" | bc -l; }
function logn(){ echo "scale=10; l($1);" | bc -l; }
function log(){ echo "scale=10; l($1)/l(10);" | bc -l; }
function pow(){ echo "scale=10; $1^$2;" | bc -l; }
function sqrt(){ echo "scale=10; sqrt($1);" | bc -l; }
function exp(){ echo "scale=10; e($1);" | bc -l; }
alias pi="echo 'scale=10; a(1)*4;' | bc -l"
export pi=$(pi)И выполнять простенькие вычисления в баше:
Код: Выделить всё
logn 33
3.4965075614
pow 10 4
10000
sqrt 1024
32.0000000000
exp 3.5
33.1154519586Вот только с простыми арифметическими операциями проблемка - без bc уже не обойтись.
RTFM
-------
KOI8-R - патриотичная кодировка
-------
KOI8-R - патриотичная кодировка
-
drBatty
- Сообщения: 8735
- Статус: GPG ID: 4DFBD1D6 дом горит, козёл не видит...
- ОС: Slackware-current
Re: $[5/2]
зачем? если кто-то не знает про bc - ему это не поможет. А если знает, то какой смысл?
К тому-же, точность 10 лично мне не всегда нужна. Точнее - никогда не нужна. Да и синусы мне были нужны в посл. раз где-то с год назад, причём bc мне не помог, проще было на C написать небольшую программку.
-
watashiwa_daredeska
- Бывший модератор
- Сообщения: 4038
- Статус: Искусственный интеллект (pre-alpha)
- ОС: Debian GNU/Linux
Re: $[5/2]
Arithmetic Expansion
The format for arithmetic expansion is:
$((expression))
The old format $[expression] is deprecated and will be removed in upcoming versions of bash.
…
The evaluation is performed according to the rules listed below under ARITHMETIC EVALUATION.
Мои розовые очки
Спасибо сказали:
-
sash-kan
- Администратор
- Сообщения: 13939
- Статус: oel ngati kameie
- ОС: GNU
Re: $[5/2]
Shell
$ man -P 'grep -B 6 -A 7 "\$\["' bash
Arithmetic Expansion
Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result.
The format for arithmetic expansion is:
$((expression))
The old format $[expression] is deprecated and will be removed in upcoming versions of bash.
The expression is treated as if it were within double quotes, but a double quote inside the parentheses is
not treated specially. All tokens in the expression undergo parameter expansion, string expansion, command
substitution, and quote removal. Arithmetic expansions may be nested.
The evaluation is performed according to the rules listed below under ARITHMETIC EVALUATION. If expression
is invalid, bash prints a message indicating failure and no substitution occurs.упс, опоздал· надо всегда нажимать f5/r перед тем, как отвечать на давным-давно загруженной странице·
Писать безграмотно - значит посягать на время людей, к которым мы адресуемся, а потому совершенно недопустимо в правильно организованном обществе. © Щерба Л. В., 1957
при сбоях форума см.блог
при сбоях форума см.блог
Спасибо сказали:
-
drBatty
- Сообщения: 8735
- Статус: GPG ID: 4DFBD1D6 дом горит, козёл не видит...
- ОС: Slackware-current
Re: $[5/2]
странно, у меня так написано:
Arithmetic Expansion
Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. The format for arithmetic expansion is:
$((expression))
The expression is treated as if it were within double quotes, but a double quote inside the parentheses is not treated specially. All tokens in the
expression undergo parameter expansion, string expansion, command substitution, and quote removal. Arithmetic expansions may be nested.
The evaluation is performed according to the rules listed below under ARITHMETIC EVALUATION. If expression is invalid, bash prints a message indicat‐
ing failure and no substitution occurs.
видать Патрег решил, что оно совсем уж deprecated...
Ну всем спасибо, понял. Как писал $(( )), так и буду писать (:
-
ZyX
- Сообщения: 355
- ОС: Gentoo
Re: $[5/2]
eddy писал(а): ↑18.08.2011 16:56Кстати, можно еще в .bashrc запихать вот это
Код: Выделить всё
function round(){ [ "$2" = "" ] && NUM=3 || NUM=$2; echo $1 | sed "s/\([[:digit:]]*\.[[:digit:]]\{,$NUM\}\).*/\1/"; } function sin(){ echo "scale=10; s($1*a(1)/45);" | bc -l; } function cos(){ echo "scale=10; c($1*a(1)/45);" | bc -l; } function tan(){ echo "scale=10; arg=$1*a(1)/45; s(arg)/c(arg);" | bc -l; } function logn(){ echo "scale=10; l($1);" | bc -l; } function log(){ echo "scale=10; l($1)/l(10);" | bc -l; } function pow(){ echo "scale=10; $1^$2;" | bc -l; } function sqrt(){ echo "scale=10; sqrt($1);" | bc -l; } function exp(){ echo "scale=10; e($1);" | bc -l; } alias pi="echo 'scale=10; a(1)*4;' | bc -l" export pi=$(pi)
И выполнять простенькие вычисления в баше:
Код: Выделить всё
logn 33 3.4965075614 pow 10 4 10000 sqrt 1024 32.0000000000 exp 3.5 33.1154519586
Вот только с простыми арифметическими операциями проблемка - без bc уже не обойтись.
zsh (Created by format.vim)
zmodload zsh/mathfunc
echo $(( sqrt(exp(log(4)))**4 )) # Will output 16.