Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,27 @@ Git tags of this fork (`1.10.0-N`) do not follow the package version declared in

| Tag | Package version | Changes |
|---|---|---|
| `1.10.0-5` | `1.14.0.post5` | `LogBase` operator. Out-of-domain, overflow and underflow errors; strict mode rejects unsupported operators; `If` and `Map` let these errors through. Details below. |
| `1.10.0-4` | `1.14.0` | `create_solver(parameters, strict=True)`: an undefined or `None` parameter raises a `MathJSONException` caused by `MathJSONUnresolvedParameter`. `IsDefined` implemented. `Str` and `Not` keep the `Constants` context. `extract_variables` reads the final expression of `Constants` and ignores `Str` / `Date` literals. |
| `1.10.0-3` | `1.14.0` | `Date` operator. |
| `1.10.0-2` | `1.14.0` | Comparison operators accept multiple operands. |
| `1.10.0-1` | `1.10.0` | `Rational` and `Ln` operators. |

## 1.10.0-5 (package version 1.14.0.post5)

### Added
- `["LogBase", x, b]`: logarithm of `x` in base `b`, exact for bases 10 and 2 (`["LogBase", 1000, 10]` is `3.0`, `["LogBase", 8, 2]` is `3.0`).
- Exceptions exported from `mathjson_solver`, found as the `e` of the `MathJSONException`:
- `MathJSONDomainError` (a `ValueError`): logarithm (`Ln`, `Log`, `Log2`, `Log10`, `LogBase`) of a number `<= 0`, base `<= 0` or `== 1`, root (`Sqrt`, `Root`) of a negative number whose exponent `1/n` is not an integer, e.g. `["Root", -16, 4]`, power of a negative number with a non-integer exponent. No result is a complex number any more.
- `MathJSONOverflowError` (an `OverflowError`): a calculation on finite numbers whose result is infinite, NaN or too large for a float, e.g. `["Power", 10, 400]`, `["Exp", 1000]`, `["Multiply", 1e300, 1e300]`. Any other `OverflowError` raised by an operator is converted to it.
- `MathJSONUnderflowError` (an `ArithmeticError`): `Power` or `Exp` of finite numbers whose non-zero result becomes `0.0`, e.g. `["Power", 10, -400]`, `["Exp", -1000]`.
- `MathJSONUnsupportedOperator` (a `NotImplementedError`): in strict mode, a formula whose operator is unknown, e.g. `["Floor", 2.5]`. A list given as a parameter value, at any depth, is data and is still returned as is.
- `Root` of a negative number with an odd index is real: `["Root", -8, 3]` is `-2.0`.

### Changed
- Infinite and NaN values coming from the parameters raise neither an overflow nor an underflow: only finite operands do. Converting an infinite value to an integer (`Round`, `Int`) still fails, now as a `MathJSONOverflowError`.
- `If` and `Map` let the errors above through instead of falling back to the `else` branch or to the element. Any other `MathJSONException` is still caught. For instance, `["If", [["Greater", ["Log10", "x"], 1], 1], 2]` with `x = 0` gave `2` and now raises a domain error.

### Unchanged
- `["Log", x]` is still the natural logarithm, whatever its base argument.
- `Power` of integers still gives an integer: `["Power", 2, 3]` is `8`.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ except MathJSONException:

Left unhandled, the exception will look like `MathJSONException("Problem in Divide. ['Divide', 1, 0]. division by zero")`.

The original error is the exception's `e`. A calculation that cannot be done gives one of these errors:
* `MathJSONDomainError` (a `ValueError`): an argument is outside the domain of the operator, e.g. `["Log10", 0]`, `["Sqrt", -4]`;
* `MathJSONOverflowError` (an `OverflowError`): finite numbers give a result too large for a float, e.g. `["Power", 10, 400]`;
* `MathJSONUnderflowError` (an `ArithmeticError`): `Power`, `Square` or `Exp` of finite numbers gives a non-zero result too small for a float, e.g. `["Exp", -1000]`.

Infinite and NaN values given as parameters raise neither an overflow nor an underflow: `inf * 100` is `inf`. Converting an infinite value to an integer (`Round`, `Int`) raises a `MathJSONOverflowError`.


## Strict mode

Expand All @@ -62,7 +69,8 @@ In strict mode:
* a parameter whose value is `None` is unresolved too;
* string values of parameters are returned as is, not resolved again as names;
* string literals must be written with `Str`: `["Str", "plasma"]` returns `"plasma"` without looking it up. Bare strings anywhere else (`Switch` cases, `In` arrays, `Strptime` formats, ...) are parameter names;
* `If`, `Map` and `HasMatchingSublist` still catch a `MathJSONException` raised by a branch or an element, as they do in the default mode.
* a formula whose operator is unknown raises a `MathJSONException` caused by `MathJSONUnsupportedOperator` (a `NotImplementedError`), instead of being returned unchanged. A list given as a parameter value is data, and is returned as is;
* `If` and `Map` still catch a `MathJSONException` raised by a branch or an element, as they do in the default mode, except the calculation errors above, which they let through in both modes.


## How to run tests
Expand Down
9 changes: 8 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* [Log](#exponents-and-logarithms)
* [Log2](#exponents-and-logarithms)
* [Log10](#exponents-and-logarithms)
* [LogBase](#exponents-and-logarithms)
* [Equal](#equality-greater-than-less-than)
* [StrictEqual](#equality-greater-than-less-than)
* [NotEqual](#equality-greater-than-less-than)
Expand Down Expand Up @@ -139,25 +140,31 @@ Performs a division.
```

#### Square and Power
`Power` raises a number to given power. `Square` is a special case of `Power`.
`Power` raises a number to given power. `Square` is a special case of `Power`. A negative number with a non-integer exponent raises a `MathJSONDomainError`, a result too large for a float a `MathJSONOverflowError`, and a non-zero result too small for a float a `MathJSONUnderflowError`.
```python
["Power", 2, 3] # 2^3=8
["Power", 10, -3] # 10^-3=0.001
["Square", 4] # 4^2=16
```

#### Root and square root
An odd root of a negative number is real. Any other root of a negative number, whose exponent `1/n` is not an integer, raises a `MathJSONDomainError`.
```python
["Root", 9, 2] # √9=3.0
["Root", 8, 3] # ∛8=2.0
["Root", -8, 3] # ∛-8=-2.0
["Sqrt", 9] # √9=3.0
```

#### Exponents and logarithms
`Log` is the natural logarithm, like `Ln`, and ignores a second argument: use `LogBase` for another base. The logarithm of a number `<= 0`, or in a base `<= 0` or equal to 1, raises a `MathJSONDomainError`.
```python
["Exp", 2] # e^2≅7.389
["Log", 2.7183] # ln(2.7183)≅1.0000
["Ln", 2.7183] # ln(2.7183)≅1.0000
["Log2", 8] # log2(8)=3.0
["Log10", 1000] # log10(1000)=3.0
["LogBase", 8, 2] # log2(8)=3.0
```

### Conditionality
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = mathjson-solver
version = 1.14.0
version = 1.14.0.post5
author = Martins Mednis
author_email = mrt@mednis.info
description = Utilities for MathJSON evaluation
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="mathjson-solver",
version="1.14.0",
version="1.14.0.post5",
author="Martins Mednis",
author_email="mrt@mednis.info",
description="Utilities for MathJSON evaluation",
Expand Down
4 changes: 4 additions & 0 deletions src/mathjson_solver/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from .__main__ import create_mathjson_solver as create_solver
from .__main__ import (
MathJSONDomainError,
MathJSONException,
MathJSONOverflowError,
MathJSONUnderflowError,
MathJSONUnresolvedParameter,
MathJSONUnsupportedOperator,
extract_variables,
)
Loading
Loading