Published
Edited
Jan 16, 2021
Fork of Float utils
Importers
Insert cell
md`# Float utils
> 计算公式 f(x) = tan(x) / scale, x 介于 [-127, 128] * unit, unit = PI / 256
需要实现:
- 转二进制形式:str toP8Bin(float)
- 二进制形式转单精度 float parseP8Bin(str)
`
Insert cell
Insert cell
Insert cell
toP8Bin = (function () {
let {atan, PI, round} = Math
const unit = PI / 255
// https://stackoverflow.com/questions/11433789/why-is-the-range-of-signed-byte-is-from-128-to-127-2s-complement-and-not-fro
const min = -128 * unit
return float => {
// [-Inf, Inf] -> [-PI/2, PI/2] -> [-128, 127] -> [0, 255]
let rad = atan(float * scale)
let uint = round((rad - min) / unit)
return intToBin(uint, 8)
}
})()
Insert cell
parseP8Bin = (function () {
const {tan, PI} = Math
const unit = PI / 255
return str => {
// [0, 255] -> [-128, 127] -> [-PI/2, PI/2] -> [-Inf, Inf]
let uint = parseInt(str, 2)
let int = uint - 128
return tan(unit * int) / scale
}
})()
Insert cell
Insert cell
toP8Bin(-1.5)
Insert cell
parseP8Bin('00001110')
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more