Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
decimal/example/integral_conversions.cpp at develop · boostorg/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 }}
Uh oh!
There was an error while loading.
Please reload this page
.
boostorg
/
decimal
Public
Notifications
You must be signed in to change notification settings
Fork
15
Star
65
Code
Issues
13
Pull requests
1
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Files
Expand file tree
develop
Breadcrumbs
decimal
/
example
/
integral_conversions.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
78 lines (62 loc) · 3.86 KB
develop
Breadcrumbs
decimal
/
example
/
integral_conversions.cpp
Copy path
Top
File metadata and controls
Code
Blame
78 lines (62 loc) · 3.86 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
//
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
<
limits
>
#
include
<
cstdint
>
int
main
()
{
using
boost::decimal::
decimal32_t
;
//
Type decimal32_t
using
boost::decimal::
decimal64_t
;
//
Type decimal64_t
//
Non-finite values construct std::numeric_limits<TargetIntegerType>::max()
constexpr
decimal64_t
decimal_qnan {std::numeric_limits<
decimal64_t
>::
quiet_NaN
()};
const
std::
uint32_t
int_from_nan {
static_cast
<std::
uint32_t
>(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) && int_from_nan == std::numeric_limits<std::
uint32_t
>::
max
())
{
std::cout <<
"
Decimal QNAN converts to Integer Max
\n
"
;
}
//
Same thing happens with decimal infinities since integers don't have an infinity
constexpr
decimal32_t
decimal_inf {std::numeric_limits<
decimal32_t
>::
infinity
()};
const
std::
uint64_t
int_from_inf {
static_cast
<std::
uint64_t
>(decimal_inf)};
//
Same as the above but with INF instead of NAN
if
(
boost::decimal::isinf
(decimal_inf) && int_from_inf == std::numeric_limits<std::
uint64_t
>::
max
())
{
std::cout <<
"
Decimal INF converts to Integer Max
\n
"
;
}
//
For finite values the construction of the resulting integer matches the behavior
//
you are familiar with from binary floating point to integer conversions.
//
Namely, the result will only have the integer component of the decimal
//
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
std::
uint32_t
int_from_pi {
static_cast
<std::
uint32_t
>(decimal_pi)};
std::cout <<
std::setprecision
(std::numeric_limits<
decimal64_t
>::digits10)
<<
"
decimal64_t pi:
"
<< decimal_pi <<
'
\n
'
<<
"
std::uint32_t pi:
"
<< int_from_pi <<
"
\n\n
"
;
//
Constructing a decimal value from an integer is lossless until
//
the number of digits in the integer exceeds the precision of the decimal type
std::cout <<
"
Conversions will be lossless
\n
"
<<
"
decimal64_t digits10:
"
<< std::numeric_limits<
decimal64_t
>::digits10 <<
"
\n
"
<<
"
std::uint32_t digits10:
"
<< std::numeric_limits<std::
uint32_t
>::digits10 <<
"
\n
"
;
constexpr
decimal64_t
decimal_from_u32_max {std::numeric_limits<std::
uint32_t
>::
max
()};
std::cout <<
"
std::uint32_t max:
"
<< std::numeric_limits<std::
uint32_t
>::
max
() <<
"
\n
"
<<
"
decimal64_t from max:
"
<< decimal_from_u32_max <<
"
\n\n
"
;
//
In the construction of lossy values the rounding will be handled according to
//
the current global rounding mode.
std::cout <<
"
Conversions will be lossy
\n
"
<<
"
decimal32_t digits10:
"
<< std::numeric_limits<
decimal32_t
>::digits10 <<
"
\n
"
<<
"
std::uint64_t digits10:
"
<< std::numeric_limits<std::
uint64_t
>::digits10 <<
"
\n
"
;
constexpr
decimal32_t
decimal_from_u64_max {std::numeric_limits<std::
uint64_t
>::
max
()};
std::cout <<
"
std::uint64_t max:
"
<< std::numeric_limits<std::
uint64_t
>::
max
() <<
"
\n
"
<<
"
decimal32_t from max:
"
<< decimal_from_u64_max <<
'
\n
'
;
return
0
;
}
You can’t perform that action at this time.