定点数数学库fpm源码走读

定点数数学库fpm(fixed-point math)

github:https://github.com/MikeLankamp/fpm

仅包含头文件的实现,需要C++11或以上。

https://github.com/TxtpGame/fpm/blob/master/docs/index.md

核心文件

fixed.hpp:提供定点数的核心逻辑

ios.hpp:提供定点数文本流的输入和输出

math.hpp:提供一些简单的数学函数

fixed.hpp

定点数的本质:移位FractionBits,将小数放大成整数

BaseType:实际存储的类型

IntermediateType:中间计算的类型

FractionBits:移位的位数,8 16 24。8相当于放大65536倍

FRACTION_MULT:实际的放大倍数

raw_construct_tag:空结构体,占位用的,函数重载,有一个特殊的构造函数内部使用

构造函数:integral、floating-point、fixed-point

类型转换函数:integral、floating-point

定点数之间的转换:考虑FractionBits的不同

定义了一些数学常量,e pi

接下来重载一些运算符-、+=、-=、*=、/=

using fixed_16_16 = fixed<std::int32_t, std::int64_t, 16>;
using fixed_24_8 = fixed<std::int32_t, std::int64_t, 8>;
using fixed_8_24 = fixed<std::int32_t, std::int64_t, 24>;

定义3种常用的类型,

接下来:定义一些运算符重载

Addition、Subtraction、Multiplication、Division

==、!=、<、>、<=、>=

接下来:实现了std::hash

接下来:实现了numeric_limits

ios.hpp

实现了 operator<<、operator>>

math.hpp

实现了一些数学函数,里面涉及到泰勒展式,挺有意思的。

posted @ 2022-04-21 10:58  天下太平  阅读(1219)  评论(0)    收藏  举报