Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4daba4b

Browse files
author
zhunago
committed
log sum exp trick
1 parent f3ff6c4 commit 4daba4b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

‎algorithm/numstab.cpp‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void overflow()
5+
{
6+
double a = exp(998);
7+
double b = exp(1000);
8+
9+
double c = 0;
10+
for(int i = 0; i < 100; ++i)
11+
{
12+
c += exp(-2);
13+
}
14+
cout << 1000 + log(c + 1) << endl;
15+
cout << log(b + a *100) << endl;
16+
}
17+
18+
void underflow()
19+
{
20+
double a = exp(-1000);
21+
double b = exp(-999);
22+
23+
double c = 0;
24+
for(int i = 0; i < 100; ++i)
25+
{
26+
c += exp(-1);
27+
}
28+
cout << -999 + log(c + 1) << endl;
29+
cout << log(b + a *100) << endl;
30+
}
31+
int main(void)
32+
{
33+
overflow();
34+
underflow();
35+
return 0;
36+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /