yq/pkg/yqlib/doc/operators/modulo.md
TJ Miller a466821e8f
Added divide and modulo operators (#1593)
* Added divide operator (#49)

* Tidy up divide operator logic

* Added modulo operator

* Fix divide test typo

* Add divide by zero test

* Handle int modulo by 0 and add tests

* Tidy up divide/modulo operator node creation

* Fix linter errors
2023-03-15 20:14:23 +11:00

958 B

Number modulo - int

If the lhs and rhs are ints then the expression will be calculated with ints.

Given a sample.yml file of:

a: 13
b: 2

then

yq '.a = .a % .b' sample.yml

will output

a: 1
b: 2

Number modulo - float

If the lhs or rhs are floats then the expression will be calculated with floats.

Given a sample.yml file of:

a: 12
b: 2.5

then

yq '.a = .a % .b' sample.yml

will output

a: !!float 2
b: 2.5

Number modulo - int by zero

If the lhs is an int and rhs is a 0 the result is an error.

Given a sample.yml file of:

a: 1
b: 0

then

yq '.a = .a % .b' sample.yml

will output

Error: cannot modulo by 0

Number modulo - float by zero

If the lhs is a float and rhs is a 0 the result is NaN.

Given a sample.yml file of:

a: 1.1
b: 0

then

yq '.a = .a % .b' sample.yml

will output

a: !!float NaN
b: 0