Skip to content

Commit

Permalink
doc for exp
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Apr 17, 2020
1 parent c072301 commit 41bdaaa
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions algo-ja.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,41 @@
# logの実装試行錯誤
# expの実装

y = exp(x)

## 計算の範囲

### floatの場合
- x = -87.3でFLT_MIN=1.17e-38
- x = 88.72でFLT_MAX=3.4e38

## 式変形

```
e^x = 2^(x log_2(e))
```

- x' = x log_2(e)
- x' = n + a (|a| <= 0.5), n : 整数と分割する
- e^x = 2^n × 2^a
- 2^a = e^(a log(2)) = e^b ; b = a log(2)

|a| <= 0.5でlog(2) = 0.693なので|b| = |a log(2)| <= 0.346.

- bが小さい値なのでこれを級数展開する
- 6次の項は0.346^6/6! = 2.4e-6とfloatの分解能に近いので5次で切る

```
e^b = 1 + b + b^2/2! + b^3/3! + b^4/4! + b^5/5!
```

## 2の巾の計算

整数nに対するfloat(2^n)を求める
対応するビットパターンはs = 0, e = n + 127, f = 0なので((n + 127) << 23)

###

## logの実装

x = y * 2^e (eは整数, 1 <= y < 2)

Expand Down Expand Up @@ -33,7 +70,11 @@ float e = (int(fi.i - (127 << 23))) >> 23;
```
とすると127 << 23の1個に減らせる.

## log(y)の計算
## floatからintへの変換

今回は近い値に切り捨てればよいのでvcvtps2dqがよい

# log(y)の計算

Maclaurin展開はlog(1 + x) = x - x^2/2 + x^3/3 + ...

Expand Down Expand Up @@ -187,4 +228,4 @@ vfmadd213ps(zm0, zm2, zm1); // log(x) = e * log(2) + log(1.5) + log(y)

が今のところ一番高速.
11clk/loop.
実装コードは[fmath2.hpp](https://github.com/herumi/fmath/blob/master/fmath2.hpp).
実装コードは[fmath2.hpp](https://github.com/herumi/fmath/blob/master/fmath2.hpp).

0 comments on commit 41bdaaa

Please sign in to comment.