-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_square_root.py
More file actions
72 lines (57 loc) · 1.92 KB
/
get_square_root.py
File metadata and controls
72 lines (57 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
Get square root of a number
"""
from __future__ import annotations
from example_fgen_basic.pyfgen_runtime.exceptions import (
CompiledExtensionNotFoundError,
FortranError,
)
from example_fgen_basic.result import ResultDP
try:
from example_fgen_basic._lib import m_get_square_root_w # type: ignore
except (ModuleNotFoundError, ImportError) as exc: # pragma: no cover
raise CompiledExtensionNotFoundError(
"example_fgen_basic._lib.m_get_square_root_w"
) from exc
try:
from example_fgen_basic._lib import m_result_dp_w
except (ModuleNotFoundError, ImportError) as exc: # pragma: no cover
raise CompiledExtensionNotFoundError(
"example_fgen_basic._lib.m_result_dp_w"
) from exc
def get_square_root(inv: float) -> float:
"""
Get square root
Parameters
----------
inv
Value for which to get the square root
Returns
-------
:
Square root of `inv`
Raises
------
FortranError
`inv` is negative
TODO: use a more specific error
"""
result_instance_index: int = m_get_square_root_w.get_square_root(inv)
result = ResultDP.from_instance_index(result_instance_index)
if result.error_v is not None:
# TODO: be more specific
raise FortranError(result.error_v.message)
# raise LessThanZeroError(result.error_v.message)
if result.data_v is None:
raise AssertionError
res = result.data_v
# TODO: think
# I like the clarity of finalising result_instance_index here
# by having an explicit call
# (so you can see creation and finalisation in same place).
# (Probably the above is my preferred right now, but we should think about it.)
# I like the safety of finalising in `from_instance_index`.
# if not finalised(result_instance_index):
# finalise(result_instance_index)
m_result_dp_w.finalise_instance(result_instance_index)
return res