Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
af8a180a
Commit
af8a180a
authored
Oct 04, 2020
by
Victor Zverovich
Committed by
Victor Zverovich
Oct 04, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make GetCachedPower test more precise
parent
a581e9e5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
17 deletions
+45
-17
include/fmt/format-inl.h
include/fmt/format-inl.h
+15
-12
test/format-impl-test.cc
test/format-impl-test.cc
+30
-5
No files found.
include/fmt/format-inl.h
View file @
af8a180a
...
@@ -1262,9 +1262,8 @@ class bigint {
...
@@ -1262,9 +1262,8 @@ class bigint {
FMT_ASSERT
(
compare
(
*
this
,
other
)
>=
0
,
""
);
FMT_ASSERT
(
compare
(
*
this
,
other
)
>=
0
,
""
);
bigit
borrow
=
0
;
bigit
borrow
=
0
;
int
i
=
other
.
exp_
-
exp_
;
int
i
=
other
.
exp_
-
exp_
;
for
(
size_t
j
=
0
,
n
=
other
.
bigits_
.
size
();
j
!=
n
;
++
i
,
++
j
)
{
for
(
size_t
j
=
0
,
n
=
other
.
bigits_
.
size
();
j
!=
n
;
++
i
,
++
j
)
subtract_bigits
(
i
,
other
.
bigits_
[
j
],
borrow
);
subtract_bigits
(
i
,
other
.
bigits_
[
j
],
borrow
);
}
while
(
borrow
>
0
)
subtract_bigits
(
i
,
0
,
borrow
);
while
(
borrow
>
0
)
subtract_bigits
(
i
,
0
,
borrow
);
remove_leading_zeros
();
remove_leading_zeros
();
}
}
...
@@ -1436,22 +1435,26 @@ class bigint {
...
@@ -1436,22 +1435,26 @@ class bigint {
exp_
*=
2
;
exp_
*=
2
;
}
}
// If this bigint has a bigger exponent than other, adds trailing zero to make
// exponents equal. This simplifies some operations such as subtraction.
void
align
(
const
bigint
&
other
)
{
int
exp_difference
=
exp_
-
other
.
exp_
;
if
(
exp_difference
<=
0
)
return
;
int
num_bigits
=
static_cast
<
int
>
(
bigits_
.
size
());
bigits_
.
resize
(
to_unsigned
(
num_bigits
+
exp_difference
));
for
(
int
i
=
num_bigits
-
1
,
j
=
i
+
exp_difference
;
i
>=
0
;
--
i
,
--
j
)
bigits_
[
j
]
=
bigits_
[
i
];
std
::
uninitialized_fill_n
(
bigits_
.
data
(),
exp_difference
,
0
);
exp_
-=
exp_difference
;
}
// Divides this bignum by divisor, assigning the remainder to this and
// Divides this bignum by divisor, assigning the remainder to this and
// returning the quotient.
// returning the quotient.
int
divmod_assign
(
const
bigint
&
divisor
)
{
int
divmod_assign
(
const
bigint
&
divisor
)
{
FMT_ASSERT
(
this
!=
&
divisor
,
""
);
FMT_ASSERT
(
this
!=
&
divisor
,
""
);
if
(
compare
(
*
this
,
divisor
)
<
0
)
return
0
;
if
(
compare
(
*
this
,
divisor
)
<
0
)
return
0
;
int
num_bigits
=
static_cast
<
int
>
(
bigits_
.
size
());
FMT_ASSERT
(
divisor
.
bigits_
[
divisor
.
bigits_
.
size
()
-
1u
]
!=
0
,
""
);
FMT_ASSERT
(
divisor
.
bigits_
[
divisor
.
bigits_
.
size
()
-
1u
]
!=
0
,
""
);
int
exp_difference
=
exp_
-
divisor
.
exp_
;
align
(
divisor
);
if
(
exp_difference
>
0
)
{
// Align bigints by adding trailing zeros to simplify subtraction.
bigits_
.
resize
(
to_unsigned
(
num_bigits
+
exp_difference
));
for
(
int
i
=
num_bigits
-
1
,
j
=
i
+
exp_difference
;
i
>=
0
;
--
i
,
--
j
)
bigits_
[
j
]
=
bigits_
[
i
];
std
::
uninitialized_fill_n
(
bigits_
.
data
(),
exp_difference
,
0
);
exp_
-=
exp_difference
;
}
int
quotient
=
0
;
int
quotient
=
0
;
do
{
do
{
subtract_aligned
(
divisor
);
subtract_aligned
(
divisor
);
...
...
test/format-impl-test.cc
View file @
af8a180a
...
@@ -212,14 +212,39 @@ TEST(FPTest, Multiply) {
...
@@ -212,14 +212,39 @@ TEST(FPTest, Multiply) {
}
}
TEST
(
FPTest
,
GetCachedPower
)
{
TEST
(
FPTest
,
GetCachedPower
)
{
typedef
std
::
numeric_limits
<
double
>
limits
;
using
limits
=
std
::
numeric_limits
<
double
>
;
for
(
auto
exp
=
limits
::
min_exponent
;
exp
<=
limits
::
max_exponent
;
++
exp
)
{
for
(
auto
exp
=
limits
::
min_exponent
;
exp
<=
limits
::
max_exponent
;
++
exp
)
{
int
dec_exp
=
0
;
int
dec_exp
=
0
;
auto
fp
=
fmt
::
detail
::
get_cached_power
(
exp
,
dec_exp
);
auto
fp
=
fmt
::
detail
::
get_cached_power
(
exp
,
dec_exp
);
EXPECT_LE
(
exp
,
fp
.
e
);
bigint
exact
,
cache
(
fp
.
f
);
int
dec_exp_step
=
8
;
if
(
dec_exp
>=
0
)
{
EXPECT_LE
(
fp
.
e
,
exp
+
dec_exp_step
*
log2
(
10
));
exact
.
assign_pow10
(
dec_exp
);
EXPECT_DOUBLE_EQ
(
pow
(
10
,
dec_exp
),
ldexp
(
static_cast
<
double
>
(
fp
.
f
),
fp
.
e
));
if
(
fp
.
e
<=
0
)
exact
<<=
-
fp
.
e
;
else
cache
<<=
fp
.
e
;
exact
.
align
(
cache
);
cache
.
align
(
exact
);
auto
exact_str
=
fmt
::
format
(
"{}"
,
exact
);
auto
cache_str
=
fmt
::
format
(
"{}"
,
cache
);
EXPECT_EQ
(
exact_str
.
size
(),
cache_str
.
size
());
EXPECT_EQ
(
exact_str
.
substr
(
0
,
15
),
cache_str
.
substr
(
0
,
15
));
int
diff
=
cache_str
[
15
]
-
exact_str
[
15
];
if
(
diff
==
1
)
EXPECT_GT
(
exact_str
[
16
],
'8'
);
else
EXPECT_EQ
(
diff
,
0
);
}
else
{
cache
.
assign_pow10
(
-
dec_exp
);
cache
*=
fp
.
f
+
1
;
// Inexact check.
exact
.
assign
(
1
);
exact
<<=
-
fp
.
e
;
exact
.
align
(
cache
);
auto
exact_str
=
fmt
::
format
(
"{}"
,
exact
);
auto
cache_str
=
fmt
::
format
(
"{}"
,
cache
);
EXPECT_EQ
(
exact_str
.
size
(),
cache_str
.
size
());
EXPECT_EQ
(
exact_str
.
substr
(
0
,
16
),
cache_str
.
substr
(
0
,
16
));
}
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment