Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ex5に「誤差を気にしなくて良い」と書いているにも関らず実際は気にする必要がある #78

Open
ncaq opened this issue Nov 10, 2018 · 10 comments
Assignees
Milestone

Comments

@ncaq
Copy link

ncaq commented Nov 10, 2018

課題に書かれている「複利計算の式」を使えば良いのですが

再帰を使って計算したり,

taashiさんのツイート: "コード calcCI :: Double -> Double -> Double -> Double calcCI _ 0 a = a calcCI r y a = calcCI r (y - 1) $ a * (1 + r / 100)"

^の代わりに**を使うと誤差が発生するようです.

誤差を許容する修正をしようとしたのですが,パット見テキストの同値性を使ってるので即座の修正は難しそうですね.

@igrep
Copy link
Member

igrep commented Nov 10, 2018

そうですね。。。基本的に初心者をターゲットにして「習ってないことを使わないでね」という不文律で作っているので、どこかにそれを明記する形でサポートですかね。。。

@fumieval
Copy link
Contributor

数値計算に丸め誤差はつきものなので、この類のテストは閾値を決め、ある程度の誤差を許すのが定石ですね。社内のDiffベースのテストも、一旦Floatにキャストしたものを出力するというアプローチを取っています

@igrep
Copy link
Member

igrep commented Nov 10, 2018

いっそ round 関数を紹介してしまうかな。。。
(それで解決する問題なのか確信が持てませんが)

@ncaq
Copy link
Author

ncaq commented Nov 10, 2018

いっそのこと有理数を使ってもらうのも手では(使えるのか?)

Data.Ratio

@igrep
Copy link
Member

igrep commented Nov 10, 2018

なるほど。型注釈で :: Rationalと書けばいけるかも。

@ncaq
Copy link
Author

ncaq commented Nov 10, 2018

とりあえず現状は入力が小数形式だから使えないですが
金の計算なら誤差が生じない有理数を使うほうがシチュエーションとしてはむしろ正しそう

@ncaq
Copy link
Author

ncaq commented Nov 10, 2018

  1. answerとユーザの出力を切り取って最初の10行ぐらいしか見ないようにする
  2. テスト関数を単純なテキスト比較ではなく比較関数を受け取るようにして誤差を許容する比較をする
  3. 金の計算で誤差が発生する浮動小数点数使うの変なので,いっそのこと課題を有理数を使うものに全面的に書き換える

などの道があると思います

@igrep
Copy link
Member

igrep commented Nov 10, 2018

何もimportしないで👇 ができたので、いけると思いますよ

> 0.5 :: Rational
1 % 2

@igrep
Copy link
Member

igrep commented Nov 10, 2018

と、思ったら read 関数でエラーになった 😢

> read "0.33" :: Rational
*** Exception: Prelude.read: no parse

@igrep
Copy link
Member

igrep commented Nov 19, 2018

こちらですが、直し方としてはrunHaskellExercise'runHaskellExerciseWithStdin に、解答が等しいかどうかを == 以外で判定するための関数を引数として追加し、それに Text.take 5 right == Text.take 5 out みたいな関数を渡せばいいんじゃないかと踏んでいます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants