Author Topic: Float vs fix math  (Read 8021 times)

0 Members and 1 Guest are viewing this topic.

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 8349
  • Country: fi
    • My home page and email address
Re: Float vs fix math
« Reply #50 on: November 02, 2025, 05:57:51 pm »
Note that \$0.67913\lvert x \rvert + 0.67913 \lvert y \rvert + 0.28130 \bigl \lvert \lvert x \rvert - \lvert y \rvert \bigr \rvert\$ is within 4% of \$\sqrt{x^2 + y^2}\$, so for 0 ≤ y ≤ x ≤ 255, (174*(x + y) + 72*(x - y) + 80) / 256 is within ±10 (4%) of sqrt(x^2 + y^2).  (27% of all possible cases are exact.)

For general 0 ≤ x ≤ 255, 0 ≤ y ≤ 255, you can use(87*(x + y) + 36*(max(x, y) - min(x, y)) + 40) / 128, using signed 16-bit or unsigned 15-bit integer operations.
 

Online langwadt

  • Super Contributor
  • ***
  • Posts: 5786
  • Country: dk
Re: Float vs fix math
« Reply #51 on: November 02, 2025, 07:35:24 pm »
Note that \$0.67913\lvert x \rvert + 0.67913 \lvert y \rvert + 0.28130 \bigl \lvert \lvert x \rvert - \lvert y \rvert \bigr \rvert\$ is within 4% of \$\sqrt{x^2 + y^2}\$, so for 0 ≤ y ≤ x ≤ 255, (174*(x + y) + 72*(x - y) + 80) / 256 is within ±10 (4%) of sqrt(x^2 + y^2).  (27% of all possible cases are exact.)

For general 0 ≤ x ≤ 255, 0 ≤ y ≤ 255, you can use(87*(x + y) + 36*(max(x, y) - min(x, y)) + 40) / 128, using signed 16-bit or unsigned 15-bit integer operations.

there are variations, https://dspguru.com/dsp/tricks/magnitude-estimator/

 

Online ledtester

  • Super Contributor
  • ***
  • Posts: 4176
  • Country: us
Re: Float vs fix math
« Reply #52 on: November 02, 2025, 07:57:18 pm »
On the topic of Pythagorean approximations, here's another one ...

2689373-0

Here x is the larger of the two values.

From this video:

https://youtu.be/NWBEA2ECX-A
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

-->