Platform
Resources
Pricing
Sign in
Get started
Bruce He
Biu biu biu...
Workspace
Fork
Published
algorithm
By
Bruce He
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
Purpose-built for displays of data
Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Try it for free
Learn more
Compare fork
Fork
View
Export
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
_
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
scale
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
toP8Bin
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
parseP8Bin
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
intToBin
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML