Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
RustPython/crates/common/src/float_ops.rs at typelock · RustPython/RustPython · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
RustPython
/
RustPython
Public
Notifications
You must be signed in to change notification settings
Fork
1.5k
Star
22.3k
Code
Issues
295
Pull requests
101
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
typelock
Breadcrumbs
RustPython
/
crates
/
common
/
src
/
float_ops.rs
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
267 lines (246 loc) · 7.57 KB
typelock
Breadcrumbs
RustPython
/
crates
/
common
/
src
/
float_ops.rs
Copy path
Top
File metadata and controls
Code
Blame
267 lines (246 loc) · 7.57 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use
core
::
f64
;
use
malachite_bigint
::
{
BigInt
,
ToBigInt
}
;
use
num_traits
::
{
Float
,
Signed
,
ToPrimitive
,
Zero
}
;
pub
const
fn
decompose_float
(
value
:
f64
)
->
(
f64
,
i32
)
{
if
0.0
== value
{
(
0.0
,
0i32
)
}
else
{
let
bits = value
.
to_bits
(
)
;
let
exponent
:
i32
=
(
(
bits >>
52
)
&
0x7ff
)
as
i32
-
1022
;
let
mantissa_bits = bits
&
(
0x000f_ffff_ffff_ffff
)
|
(
1022
<<
52
)
;
(
f64
::
from_bits
(
mantissa_bits
)
,
exponent
)
}
}
/// Equate an integer to a float.
///
/// Returns true if and only if, when converted to each others types, both are equal.
///
/// # Examples
///
/// ```
/// use malachite_bigint::BigInt;
/// use rustpython_common::float_ops::eq_int;
/// let a = 1.0f64;
/// let b = BigInt::from(1);
/// let c = 2.0f64;
/// assert!(eq_int(a, &b));
/// assert!(!eq_int(c, &b));
/// ```
///
pub
fn
eq_int
(
value
:
f64
,
other
:
&
BigInt
)
->
bool
{
if
let
(
Some
(
self_int
)
,
Some
(
other_float
)
)
=
(
value
.
to_bigint
(
)
,
other
.
to_f64
(
)
)
{
value == other_float && self_int ==
*
other
}
else
{
false
}
}
pub
fn
lt_int
(
value
:
f64
,
other_int
:
&
BigInt
)
->
bool
{
match
(
value
.
to_bigint
(
)
,
other_int
.
to_f64
(
)
)
{
(
Some
(
self_int
)
,
Some
(
other_float
)
)
=> value < other_float || self_int <
*
other_int
,
// finite float, other_int too big for float,
// the result depends only on other_int’s sign
(
Some
(
_
)
,
None
)
=> other_int
.
is_positive
(
)
,
// infinite float must be bigger or lower than any int, depending on its sign
_
if
value
.
is_infinite
(
)
=> value
.
is_sign_negative
(
)
,
// NaN, always false
_ =>
false
,
}
}
pub
fn
gt_int
(
value
:
f64
,
other_int
:
&
BigInt
)
->
bool
{
match
(
value
.
to_bigint
(
)
,
other_int
.
to_f64
(
)
)
{
(
Some
(
self_int
)
,
Some
(
other_float
)
)
=> value > other_float || self_int >
*
other_int
,
// finite float, other_int too big for float,
// the result depends only on other_int’s sign
(
Some
(
_
)
,
None
)
=> other_int
.
is_negative
(
)
,
// infinite float must be bigger or lower than any int, depending on its sign
_
if
value
.
is_infinite
(
)
=> value
.
is_sign_positive
(
)
,
// NaN, always false
_ =>
false
,
}
}
pub
const
fn
div
(
v1
:
f64
,
v2
:
f64
)
->
Option
<
f64
>
{
if
v2 !=
0.0
{
Some
(
v1 / v2
)
}
else
{
None
}
}
pub
fn
mod_
(
v1
:
f64
,
v2
:
f64
)
->
Option
<
f64
>
{
if
v2 !=
0.0
{
let
val = v1 % v2
;
match
(
v1
.
signum
(
)
as
i32
,
v2
.
signum
(
)
as
i32
)
{
(
1
,
1
)
|
(
-
1
,
-
1
)
=>
Some
(
val
)
,
_
if
(
v1 ==
0.0
)
||
(
v1
.
abs
(
)
== v2
.
abs
(
)
)
=>
Some
(
val
.
copysign
(
v2
)
)
,
_ =>
Some
(
(
val + v2
)
.
copysign
(
v2
)
)
,
}
}
else
{
None
}
}
pub
fn
floordiv
(
v1
:
f64
,
v2
:
f64
)
->
Option
<
f64
>
{
if
v2 !=
0.0
{
Some
(
(
v1 / v2
)
.
floor
(
)
)
}
else
{
None
}
}
pub
fn
divmod
(
v1
:
f64
,
v2
:
f64
)
->
Option
<
(
f64
,
f64
)
>
{
if
v2 !=
0.0
{
let
mut
m = v1 % v2
;
let
mut
d =
(
v1 - m
)
/ v2
;
if
v2
.
is_sign_negative
(
)
!= m
.
is_sign_negative
(
)
{
m += v2
;
d -=
1.0
;
}
Some
(
(
d
,
m
)
)
}
else
{
None
}
}
// nextafter algorithm based off of https://gitlab.com/bronsonbdevost/next_afterf
#
[
allow
(
clippy
::
float_cmp
)
]
pub
fn
nextafter
(
x
:
f64
,
y
:
f64
)
->
f64
{
if
x == y
{
y
}
else
if
x
.
is_nan
(
)
|| y
.
is_nan
(
)
{
f64
::
NAN
}
else
if
x >= f64
::
INFINITY
{
f64
::
MAX
}
else
if
x <= f64
::
NEG_INFINITY
{
f64
::
MIN
}
else
if
x ==
0.0
{
f64
::
from_bits
(
1
)
.
copysign
(
y
)
}
else
{
// next x after 0 if y is farther from 0 than x, otherwise next towards 0
// the sign is a separate bit in floats, so bits+1 moves away from 0 no matter the float
let
b = x
.
to_bits
(
)
;
let
bits =
if
(
y > x
)
==
(
x >
0.0
)
{
b +
1
}
else
{
b -
1
}
;
let
ret = f64
::
from_bits
(
bits
)
;
if
ret ==
0.0
{
ret
.
copysign
(
x
)
}
else
{
ret
}
}
}
#
[
allow
(
clippy
::
float_cmp
)
]
pub
fn
nextafter_with_steps
(
x
:
f64
,
y
:
f64
,
steps
:
u64
)
->
f64
{
if
x == y
{
y
}
else
if
x
.
is_nan
(
)
|| y
.
is_nan
(
)
{
f64
::
NAN
}
else
if
x >= f64
::
INFINITY
{
f64
::
MAX
}
else
if
x <= f64
::
NEG_INFINITY
{
f64
::
MIN
}
else
if
x ==
0.0
{
f64
::
from_bits
(
1
)
.
copysign
(
y
)
}
else
{
if
steps ==
0
{
return
x
;
}
if
x
.
is_nan
(
)
{
return
x
;
}
if
y
.
is_nan
(
)
{
return
y
;
}
let
sign_bit
:
u64
=
1
<<
63
;
let
mut
ux = x
.
to_bits
(
)
;
let
uy = y
.
to_bits
(
)
;
let
ax = ux
&
!sign_bit
;
let
ay = uy
&
!sign_bit
;
// If signs are different
if
(
(
ux ^ uy
)
&
sign_bit
)
!=
0
{
return
if
ax + ay <= steps
{
f64
::
from_bits
(
uy
)
}
else
if
ax < steps
{
let
result =
(
uy
&
sign_bit
)
|
(
steps - ax
)
;
f64
::
from_bits
(
result
)
}
else
{
ux -= steps
;
f64
::
from_bits
(
ux
)
}
;
}
// If signs are the same
if
ax > ay
{
if
ax - ay >= steps
{
ux -= steps
;
f64
::
from_bits
(
ux
)
}
else
{
f64
::
from_bits
(
uy
)
}
}
else
if
ay - ax >= steps
{
ux += steps
;
f64
::
from_bits
(
ux
)
}
else
{
f64
::
from_bits
(
uy
)
}
}
}
pub
fn
ulp
(
x
:
f64
)
->
f64
{
if
x
.
is_nan
(
)
{
return
x
;
}
let
x = x
.
abs
(
)
;
let
x2 =
nextafter
(
x
,
f64
::
INFINITY
)
;
if
x2
.
is_infinite
(
)
{
// special case: x is the largest positive representable float
let
x2 =
nextafter
(
x
,
f64
::
NEG_INFINITY
)
;
x - x2
}
else
{
x2 - x
}
}
pub
fn
round_float_digits
(
x
:
f64
,
ndigits
:
i32
)
->
Option
<
f64
>
{
let
float =
if
ndigits
.
is_zero
(
)
{
let
fract = x
.
fract
(
)
;
if
(
fract
.
abs
(
)
-
0.5
)
.
abs
(
)
< f64
::
EPSILON
{
if
x
.
trunc
(
)
%
2.0
==
0.0
{
x - fract
}
else
{
x + fract
}
}
else
{
x
.
round
(
)
}
}
else
{
const
NDIGITS_MAX
:
i32
=
(
(
f64
::
MANTISSA_DIGITS
as
i32
- f64
::
MIN_EXP
)
as
f64
*
f64
::
consts
::
LOG10_2
)
as
i32
;
const
NDIGITS_MIN
:
i32
= -
(
(
(
f64
::
MAX_EXP
+
1
)
as
f64
*
f64
::
consts
::
LOG10_2
)
as
i32
)
;
if
ndigits >
NDIGITS_MAX
{
x
}
else
if
ndigits <
NDIGITS_MIN
{
0.0f64
.
copysign
(
x
)
}
else
{
let
(
y
,
pow1
,
pow2
)
=
if
ndigits >=
0
{
// according to cpython: pow1 and pow2 are each safe from overflow, but
// pow1*pow2 ~= pow(10.0, ndigits) might overflow
let
(
pow1
,
pow2
)
=
if
ndigits >
22
{
(
10.0
.
powf
(
(
ndigits -
22
)
as
f64
)
,
1e22
)
}
else
{
(
10.0
.
powf
(
ndigits
as
f64
)
,
1.0
)
}
;
let
y =
(
x
*
pow1
)
*
pow2
;
if
!y
.
is_finite
(
)
{
return
Some
(
x
)
;
}
(
y
,
pow1
,
Some
(
pow2
)
)
}
else
{
let
pow1 =
10.0
.
powf
(
(
-ndigits
)
as
f64
)
;
(
x / pow1
,
pow1
,
None
)
}
;
let
z = y
.
round
(
)
;
#
[
allow
(
clippy
::
float_cmp
)
]
let
z =
if
(
y - z
)
.
abs
(
)
==
0.5
{
2.0
*
(
y /
2.0
)
.
round
(
)
}
else
{
z
}
;
let
z =
if
let
Some
(
pow2
)
= pow2
{
// ndigits >= 0
(
z / pow2
)
/ pow1
}
else
{
z
*
pow1
}
;
if
!z
.
is_finite
(
)
{
// overflow
return
None
;
}
z
}
}
;
Some
(
float
)
}
You can’t perform that action at this time.