Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
micron.cpp/examples/math.cpp at master · rfgplk/micron.cpp · 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 }}
rfgplk
/
micron.cpp
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
12
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
micron.cpp
/
examples
/
math.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
153 lines (133 loc) · 7.18 KB
master
Breadcrumbs
micron.cpp
/
examples
/
math.cpp
Copy path
Top
File metadata and controls
Code
Blame
153 lines (133 loc) · 7.18 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
//
math.cpp
//
Tour of micron::math (src/math/) — micron's scalar / linear-algebra
//
math layer. micron does not include <cmath>; everything here is
//
computed in pure C++ via constexpr-friendly bit-twiddling and a
//
table-driven Ryu-like layer.
//
//
What this example shows:
//
- constants: pi / e / sqrt2 / log2e via math::constant_*
//
- scalars: sqrt, log, exp, pow, fabs, fmin, fmax, fclamp
//
- branchless: abs / sign / clamp without conditional jumps
//
- ratios: compile-time rational numbers (kilo/milli/...)
//
- linalg: vec<T,N> dot/cross/norm/normalize, mat<T,R,C>
//
- quaternions: quat<F> normalize/norm
//
//
Major STL deltas:
//
- No <cmath> — micron's math is a first-party implementation.
//
- Constants are templated: constant_pi<f64> vs M_PI macro.
//
- Branchless toolkit lives at src/math/branchless.hpp; the names
//
are abs8 / abs16 / abs32 / abs64 (per-width).
//
- vec / mat / quat are *fixed-size, alignas-controlled* aggregates,
//
not heap-allocated. dynvec / dynmat are the heap variants.
//
- linalg ops (dot, cross, norm, normalize) operate on those types
//
and live in math::linalg::ops.
#
include
"
../src/io/console.hpp
"
#
include
"
../src/math/branchless.hpp
"
#
include
"
../src/math/constants.hpp
"
#
include
"
../src/math/generic.hpp
"
#
include
"
../src/math/linalg.hpp
"
#
include
"
../src/math/log.hpp
"
#
include
"
../src/math/ratios.hpp
"
#
include
"
../src/math/sqrt.hpp
"
#
include
"
../src/math/trig.hpp
"
int
main
()
{
//
================================================================
//
1. Constants
//
----------------------------------------------------------------
//
Templated by float type so you pick the precision at the call site.
//
================================================================
micron::io::println
(
"
-- 1. constants --
"
);
micron::io::println
(
"
pi =
"
, micron::math::constant_pi<
f64
>);
micron::io::println
(
"
e =
"
, micron::math::constant_e<
f64
>);
micron::io::println
(
"
sqrt2 =
"
, micron::math::constant_sqrt2<
f64
>);
micron::io::println
(
"
ln2 =
"
, micron::math::constant_ln2<
f64
>);
micron::io::println
(
"
log2e =
"
, micron::math::constant_log2e<
f64
>);
//
================================================================
//
2. Scalar functions: sqrt, log, exp, pow
//
----------------------------------------------------------------
//
Each of these has overloads for f32 / f64 / long double.
//
Most are constexpr-friendly so they evaluate at compile time
//
when the input is a literal.
//
================================================================
micron::io::println
(
"
-- 2. scalar fns --
"
);
micron::io::println
(
"
sqrt(2) =
"
,
micron::math::fsqrt
(
2.0
));
micron::io::println
(
"
fsqrt(9.0) =
"
,
micron::math::fsqrt
(
9.0
));
micron::io::println
(
"
log(e) =
"
,
micron::math::log
(micron::math::constant_e<
f64
>));
micron::io::println
(
"
exp(1) =
"
,
micron::math::exp
(
1.0
));
micron::io::println
(
"
pow(2, 10) =
"
,
micron::math::pow
(
2.0
,
10
));
//
================================================================
//
3. fmin / fmax / fclamp
//
----------------------------------------------------------------
//
NaN-aware floating-point min/max. fclamp pins x into [lo, hi].
//
================================================================
micron::io::println
(
"
-- 3. fmin/fmax/fclamp --
"
);
micron::io::println
(
"
fmin(3.0, 5.0) =
"
, micron::math::fmin<
f64
>(
3.0
,
5.0
));
micron::io::println
(
"
fmax(3.0, 5.0) =
"
, micron::math::fmax<
f64
>(
3.0
,
5.0
));
micron::io::println
(
"
fclamp(15, 0, 10) =
"
, micron::math::fclamp<
f64
>(
15.0
,
0.0
,
10.0
));
//
================================================================
//
4. Branchless integer primitives
//
----------------------------------------------------------------
//
micron::math::branchless:: — abs/sign/min/max with no jumps.
//
Useful in hot loops where branch mispredict would dominate.
//
================================================================
micron::io::println
(
"
-- 4. branchless --
"
);
micron::io::println
(
"
abs32(-7) =
"
,
micron::math::branchless::abs32
(-
7
));
micron::io::println
(
"
abs64(-1234567) =
"
,
micron::math::branchless::abs64
(-
1234567LL
));
//
================================================================
//
5. ratios — compile-time rationals (SI prefix style)
//
----------------------------------------------------------------
//
ratio<N, D> stores num/denom as constexpr static members. Aliases
//
exist for SI prefixes: kilo, mega, giga, milli, micro, nano, ...
//
================================================================
micron::io::println
(
"
-- 5. ratios --
"
);
micron::io::println
(
"
kilo =
"
, micron::kilo::num,
"
/
"
, micron::kilo::denom);
micron::io::println
(
"
milli =
"
, micron::milli::num,
"
/
"
, micron::milli::denom);
micron::io::println
(
"
nano =
"
, micron::nano::num,
"
/
"
, micron::nano::denom);
//
================================================================
//
6. vec<T, N> — fixed-size, alignas-controlled vector
//
----------------------------------------------------------------
//
Aggregate type with .data[N]. Linalg ops live in linalg::.
//
================================================================
micron::io::println
(
"
-- 6. vec --
"
);
micron::math::vec<
f64
,
3
> u{
1.0
,
2.0
,
3.0
};
micron::math::vec<
f64
,
3
> v{
4.0
,
5.0
,
6.0
};
//
dot product
f64
d =
micron::math::linalg::ops::dot
(u, v);
micron::io::println
(
"
dot(u,v) =
"
, d);
//
cross product (3-vectors only)
auto
x =
micron::math::linalg::ops::cross
(u, v);
micron::io::println
(
"
cross(u,v) = [
"
, x[
0
],
"
,
"
, x[
1
],
"
,
"
, x[
2
],
"
]
"
);
//
norm / normalize
f64
n =
micron::math::linalg::ops::norm
(u);
auto
un =
micron::math::linalg::ops::normalize
(u);
micron::io::println
(
"
|u| =
"
, n);
micron::io::println
(
"
normalize(u)= [
"
, un[
0
],
"
,
"
, un[
1
],
"
,
"
, un[
2
],
"
]
"
);
//
================================================================
//
7. mat<T, R, C> — fixed-size matrix
//
----------------------------------------------------------------
//
Aggregate row-major matrix. transpose returns a mat<T, C, R>.
//
================================================================
micron::io::println
(
"
-- 7. mat --
"
);
micron::math::mat<
f64
,
2
,
3
> m{
1.0
,
2.0
,
3.0
,
4.0
,
5.0
,
6.0
};
auto
mt =
micron::math::linalg::ops::transpose
(m);
micron::io::println
(
"
m.at(0,0)=
"
, m.
at
(
0
,
0
),
"
m.at(1,2)=
"
, m.
at
(
1
,
2
));
micron::io::println
(
"
mt.at(0,0)=
"
, mt.
at
(
0
,
0
),
"
mt.at(2,1)=
"
, mt.
at
(
2
,
1
));
//
================================================================
//
8. Where to look next
//
----------------------------------------------------------------
//
src/math/linalg/decomp.hpp — LU / QR / Cholesky factorisations
//
src/math/linalg/ops.hpp — matmul, vec ops, quaternion ops
//
src/math/blas/ — BLAS levels 1/2/3
//
src/math/quants/ — quat, dynvec, fixed-size vec
//
src/math/splines/ — bspline / bezier / catmull-rom
//
src/math/integrate/ — quadrature
//
src/math/rng/ — seeded RNG (xoshiro/pcg/...)
//
src/math/simd/ — vectorised exp/log/trig
//
================================================================
return
0
;
}
You can’t perform that action at this time.