Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
decimal/examples/binary_float_conversions.cpp at develop · samd2/decimal · 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 }}
samd2
/
decimal
Public
forked from
boostorg/decimal
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
develop
Breadcrumbs
decimal
/
examples
/
binary_float_conversions.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
79 lines (64 loc) · 3.84 KB
develop
Breadcrumbs
decimal
/
examples
/
binary_float_conversions.cpp
Copy path
Top
File metadata and controls
Code
Blame
79 lines (64 loc) · 3.84 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
//
Copyright 2025 Matt Borland
//
Distributed under the Boost Software License, Version 1.0.
//
https://www.boost.org/LICENSE_1_0.txt
//
//
This file demonstrates how to convert various types to decimal types and back,
//
along with edge case handling
#
include
<
boost/decimal/decimal32_t.hpp
>
//
For type decimal32_t
#
include
<
boost/decimal/decimal64_t.hpp
>
//
For type decimal64_t
#
include
<
boost/decimal/cmath.hpp
>
//
For decimal support of cmath functions
#
include
<
boost/decimal/iostream.hpp
>
//
For decimal support of <iostream> and <iomanip>
#
include
<
boost/decimal/numbers.hpp
>
//
For decimal support of <numbers>
#
include
<
iostream
>
#
include
<
cmath
>
#
include
<
limits
>
int
main
()
{
using
boost::decimal::
decimal32_t
;
//
Type decimal32_t
using
boost::decimal::
decimal64_t
;
//
Type decimal64_t
//
Non-finite values construct the equivalent non-finite value in binary floating point
constexpr
decimal64_t
decimal_qnan {std::numeric_limits<
decimal64_t
>::
quiet_NaN
()};
const
double
double_from_qnan {
static_cast
<
double
>(decimal_qnan)};
//
Note here that we must use boost::decimal::isnan for decimal types,
//
as it is illegal to overload std::isnan
if
(
boost::decimal::isnan
(decimal_qnan) &&
std::isnan
(double_from_qnan))
{
std::cout <<
"
Decimal QNAN converts to double QNAN
\n
"
;
}
constexpr
decimal64_t
decimal_inf {std::numeric_limits<
decimal64_t
>::
infinity
()};
const
double
double_from_inf {
static_cast
<
double
>(decimal_inf)};
//
Same as the above but with INF instead of NAN
if
(
boost::decimal::isinf
(decimal_inf) &&
std::isinf
(double_from_inf))
{
std::cout <<
"
Decimal INFINITY converts to double INFINITY
\n
"
;
}
//
For finite values we make a best effort approach to covert to double
//
We are able to decompose the decimal floating point value into a sign, significand, and exponent.
//
From there we use the methods outline in Daniel Lemire's "Number Parsing at a Gigabyte a Second",
//
to construct the binary floating point value.
//
See: https://arxiv.org/pdf/2101.11408
//
Construct the decimal64_t version of pi using our pre-computed constants from <boost/decimal/numbers.hpp>
constexpr
decimal64_t
decimal_pi {boost::decimal::numbers::pi_v<
decimal64_t
>};
const
double
double_from_pi {
static_cast
<
double
>(decimal_pi)};
std::cout <<
std::setprecision
(std::numeric_limits<
decimal64_t
>::digits10)
<<
"
decimal64_t pi:
"
<< decimal_pi <<
'
\n
'
<<
"
double pi:
"
<< double_from_pi <<
'
\n
'
;
//
To construct a decimal64_t from double we use the methods described in "Ryu: fast float-to-string conversion"
//
See: https://dl.acm.org/doi/10.1145/3192366.3192369
//
This paper shows how to decompose a double into it's sign, significand, and exponent
//
Once we have those components we can use the normal constructors of the decimal types to construct
//
Since we are using the normal constructors here,
//
any construction from this conversion is subject to the current rounding mode
//
Such as with a lossy conversion like shown (double -> decimal32_t)
const
decimal64_t
decimal_from_double {
static_cast
<
decimal64_t
>(double_from_pi)};
const
decimal32_t
lossy_decimal_from_double {
static_cast
<
decimal32_t
>(double_from_pi)};
std::cout <<
"
Converted pi:
"
<< decimal_from_double <<
'
\n
'
<<
"
decimal32_t pi:
"
<< lossy_decimal_from_double <<
'
\n
'
;
//
Other than what has already been shown,
//
there are no other ways in the library to convert between decimal types and binary floating point types
//
The reason for this is to discourage their use.
//
//
You can use intermediate representations like strings if you want to make these conversions,
//
and want to be sure about what the resulting value will be
return
0
;
}
You can’t perform that action at this time.