-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfloat.mbt
More file actions
106 lines (93 loc) · 2.69 KB
/
Copy pathfloat.mbt
File metadata and controls
106 lines (93 loc) · 2.69 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2025 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///|
pub struct PyFloat {
priv obj : PyObject
}
///|
/// Create a python float object from a python object.
/// If the python object is not a float, it will raise a TypeMismatchError.
pub fn PyFloat::create(obj : PyObject) -> PyFloat raise PyRuntimeError {
guard obj.is_float() else { raise TypeMismatchError }
PyFloat::{ obj, }
}
///|
fn PyFloat::create_unchecked(obj : PyObject) -> PyFloat {
PyFloat::{ obj, }
}
///|
/// Ceate a python float object from a python object reference.
/// If the python object is not a float, it will raise a TypeMismatchError.
pub fn PyFloat::create_by_ref(
obj : @cpython.PyObjectRef,
) -> PyFloat raise PyRuntimeError {
guard @cpython.py_float_check(obj) else { raise TypeMismatchError }
PyFloat::{ obj: PyObject::create(obj) }
}
///|
fn PyFloat::create_by_ref_unchecked(obj : @cpython.PyObjectRef) -> PyFloat {
PyFloat::{ obj: PyObject::create(obj) }
}
///|
test {
ignore(PyFloat::create_by_ref_unchecked)
}
///|
/// Create a PyFloat from a Double value.
///
/// ## Example
///
/// ```moonbit
/// let f = @python.PyFloat::from(3.5);
/// inspect(f, content="3.5")
/// ```
pub fn PyFloat::from(value : Double) -> PyFloat {
PyFloat::{ obj: @cpython.py_float_from_double(value) |> PyObject::create }
}
///|
/// Convert a PyFloat to a Double.
///
/// ## Example
///
/// ```moonbit
/// let f = @python.PyFloat::from(3.5);
/// assert_eq(f.to_double(), 3.5);
/// ```
pub fn PyFloat::to_double(self : PyFloat) -> Double {
@cpython.py_float_as_double(self.obj_ref())
}
///|
/// Print the PyFloat object direcly.
///
/// Different from use `println`, `dump` means we made python interpreter
/// print the object directly.
pub fn PyFloat::dump(self : PyFloat) -> Unit {
PyObject::dump(self.obj)
}
///|
pub fn PyFloat::drop(self : PyFloat) -> Unit {
self.obj.drop()
}
///|
pub impl IsPyObject for PyFloat with obj(self) {
self.obj
}
///|
pub impl Show for PyFloat with to_string(self) -> String {
@cpython.py_object_moonbit_repr(self.obj_ref())
}
///|
pub impl Show for PyFloat with output(self : PyFloat, logger : &Logger) -> Unit {
logger.write_string(self.to_string())
}