回答編集履歴

10

replace context

2022/10/11 08:54

投稿

ps_aux_grep
ps_aux_grep

スコア1579

test CHANGED
@@ -1,6 +1,6 @@
1
1
  本質的な[解答はozwkさんのもの](https://teratail.com/questions/k9tuqlfp39opgu#reply-dza4fnuvj8usp6)を参照してください.余談までに誤差を抑える方法を示しておきます.
2
2
  ### 1. 分数を定義して計算する
3
- 分母も分子も整数で表現できる場合,自分で分数`Fraction`を定義して整数のまま四則演算を行うことで誤差を抑えることができます.手打ちで小数を入力できるということは,10進数で有限桁の小数のはずなので,分母分子共に整数にすることができるはずです.
3
+ 分母も分子も整数で表現できる場合,自分で分数`Fraction`を定義して整数のまま四則演算を行うことで誤差を抑えることができます.手打ちで小数を入力できるということは,10進数で有限桁の小数のはずなので,分母分子共に整数にすることができるはずです.上の例では`0.3`を`3/10`のようにできます.
4
4
 
5
5
  ```Python
6
6
  class Fraction:
@@ -103,4 +103,4 @@
103
103
  ```Python
104
104
  print(int("3")+3)
105
105
  ```
106
- `int()`関数に与えられた文字列が,整数と解釈できる場合には整数に変換してくれます.よって少数であるような文字列`".3"`などを`int(".3")`として使うとエラーになります.この場合は浮動小数点数として解釈できるので`float()`関数を使って`float(".3")`を実行することができます.上の例では`0.3`を`3/10`のようにできます.
106
+ `int()`関数に与えられた文字列が,整数と解釈できる場合には整数に変換してくれます.よって少数であるような文字列`".3"`などを`int(".3")`として使うとエラーになります.この場合は浮動小数点数として解釈できるので`float()`関数を使って`float(".3")`を実行することができます.

9

fix context

2022/10/11 08:53

投稿

ps_aux_grep
ps_aux_grep

スコア1579

test CHANGED
@@ -1,6 +1,6 @@
1
1
  本質的な[解答はozwkさんのもの](https://teratail.com/questions/k9tuqlfp39opgu#reply-dza4fnuvj8usp6)を参照してください.余談までに誤差を抑える方法を示しておきます.
2
2
  ### 1. 分数を定義して計算する
3
- 分母も分子も整数で表現できる場合,自分で分数`Fraction`を定義して整数のまま四則演算を行うことで誤差を抑えることができます.
3
+ 分母も分子も整数で表現できる場合,自分で分数`Fraction`を定義して整数のまま四則演算を行うことで誤差を抑えることができます.手打ちで小数を入力できるということは,10進数で有限桁の小数のはずなので,分母分子共に整数にすることができるはずです.
4
4
 
5
5
  ```Python
6
6
  class Fraction:
@@ -103,4 +103,4 @@
103
103
  ```Python
104
104
  print(int("3")+3)
105
105
  ```
106
- `int()`関数に与えられた文字列が,整数と解釈できる場合には整数に変換してくれます.よって少数であるような文字列`".3"`などを`int(".3")`として使うとエラーになります.この場合は浮動小数点数として解釈できるので`float()`関数を使って`float(".3")`を実行することができます.
106
+ `int()`関数に与えられた文字列が,整数と解釈できる場合には整数に変換してくれます.よって少数であるような文字列`".3"`などを`int(".3")`として使うとエラーになります.この場合は浮動小数点数として解釈できるので`float()`関数を使って`float(".3")`を実行することができます.上の例では`0.3`を`3/10`のようにできます.

8

fixed title decimals to decimal

2022/10/11 08:49

投稿

ps_aux_grep
ps_aux_grep

スコア1579

test CHANGED
@@ -78,7 +78,7 @@
78
78
  2/5 * 3/1 = 6/5 decimals: 1.2
79
79
  ```
80
80
 
81
- ### 2. Decimalsを使う
81
+ ### 2. Decimalを使う
82
82
  標準ライブラリ[decimal](https://docs.python.org/ja/3/library/decimal.html)を使う.詳細はリンクを読んでください.
83
83
  ```Python
84
84
  from decimal import Decimal

7

append link

2022/10/11 08:48

投稿

ps_aux_grep
ps_aux_grep

スコア1579

test CHANGED
@@ -1,4 +1,4 @@
1
- 本質的な解答はozwkさんのものを参照してください.余談までに誤差を抑える方法を示しておきます.
1
+ 本質的な[解答はozwkさんのもの](https://teratail.com/questions/k9tuqlfp39opgu#reply-dza4fnuvj8usp6)を参照してください.余談までに誤差を抑える方法を示しておきます.
2
2
  ### 1. 分数を定義して計算する
3
3
  分母も分子も整数で表現できる場合,自分で分数`Fraction`を定義して整数のまま四則演算を行うことで誤差を抑えることができます.
4
4
 

6

fixed code

2022/10/11 08:46

投稿

ps_aux_grep
ps_aux_grep

スコア1579

test CHANGED
@@ -50,10 +50,10 @@
50
50
  return float(self // other)
51
51
 
52
52
  def __str__(self):
53
- return "{}/{}".format(self.n, self.d)
53
+ return f"{self.n}/{self.d}"
54
54
 
55
55
  def __repr__(self):
56
- return "Fraction({}, {})".format(self.n, self.d)
56
+ return f"Fraction({self.n}, {self.d})"
57
57
 
58
58
  def __float__(self):
59
59
  return self.n / self.d
@@ -88,7 +88,6 @@
88
88
 
89
89
  if __name__ == "__main__":
90
90
  show_product(Decimal("0.3"), Decimal("3"))
91
-
92
91
  show_product(Decimal("0.4"), Decimal("3"))
93
92
  ```
94
93
 

5

fixed code

2022/10/11 08:44

投稿

ps_aux_grep
ps_aux_grep

スコア1579

test CHANGED
@@ -53,7 +53,7 @@
53
53
  return "{}/{}".format(self.n, self.d)
54
54
 
55
55
  def __repr__(self):
56
- return "{}/{} : {}".format(self.n, self.d, float(self))
56
+ return "Fraction({}, {})".format(self.n, self.d)
57
57
 
58
58
  def __float__(self):
59
59
  return self.n / self.d
@@ -87,15 +87,9 @@
87
87
  print(f"{r1} * {r2} = {r1 * r2}")
88
88
 
89
89
  if __name__ == "__main__":
90
- show_product(
91
- Decimal("0.3"),
90
+ show_product(Decimal("0.3"), Decimal("3"))
92
- Decimal("3")
93
- )
94
91
 
95
- show_product(
96
- Decimal("0.4"),
92
+ show_product(Decimal("0.4"), Decimal("3"))
97
- Decimal("3")
98
- )
99
93
  ```
100
94
 
101
95
 

4

fixed code

2022/10/11 08:29

投稿

ps_aux_grep
ps_aux_grep

スコア1579

test CHANGED
@@ -74,8 +74,8 @@
74
74
  ```
75
75
 
76
76
  ```shell:標準出力
77
- 10/3 * 1/3 = 9/10 decimals: 0.9
77
+ 3/10 * 3/1 = 9/10 decimals: 0.9
78
- 5/2 * 1/3 = 6/5 decimals: 1.2
78
+ 2/5 * 3/1 = 6/5 decimals: 1.2
79
79
  ```
80
80
 
81
81
  ### 2. Decimalsを使う

3

fixed code

2022/10/11 08:28

投稿

ps_aux_grep
ps_aux_grep

スコア1579

test CHANGED
@@ -50,10 +50,10 @@
50
50
  return float(self // other)
51
51
 
52
52
  def __str__(self):
53
- return "{}/{}".format(self.d, self.n)
53
+ return "{}/{}".format(self.n, self.d)
54
54
 
55
55
  def __repr__(self):
56
- return "{}/{} : {}".format(self.d, self.n, float(self))
56
+ return "{}/{} : {}".format(self.n, self.d, float(self))
57
57
 
58
58
  def __float__(self):
59
59
  return self.n / self.d
@@ -74,8 +74,8 @@
74
74
  ```
75
75
 
76
76
  ```shell:標準出力
77
- 10/3 * 1/3 = 10/9 decimals: 0.9
77
+ 10/3 * 1/3 = 9/10 decimals: 0.9
78
- 5/2 * 1/3 = 5/6 decimals: 1.2
78
+ 5/2 * 1/3 = 6/5 decimals: 1.2
79
79
  ```
80
80
 
81
81
  ### 2. Decimalsを使う

2

append prefix

2022/10/11 08:06

投稿

ps_aux_grep
ps_aux_grep

スコア1579

test CHANGED
@@ -1,4 +1,4 @@
1
- 余談までに誤差を抑える方法を示しておきます.
1
+ 本質的な解答はozwkさんのものを参照してください.余談までに誤差を抑える方法を示しておきます.
2
2
  ### 1. 分数を定義して計算する
3
3
  分母も分子も整数で表現できる場合,自分で分数`Fraction`を定義して整数のまま四則演算を行うことで誤差を抑えることができます.
4
4
 

1

fixed context

2022/10/11 08:04

投稿

ps_aux_grep
ps_aux_grep

スコア1579

test CHANGED
@@ -78,7 +78,7 @@
78
78
  5/2 * 1/3 = 5/6 decimals: 1.2
79
79
  ```
80
80
 
81
- ### Decimalsを使う
81
+ ### 2. Decimalsを使う
82
82
  標準ライブラリ[decimal](https://docs.python.org/ja/3/library/decimal.html)を使う.詳細はリンクを読んでください.
83
83
  ```Python
84
84
  from decimal import Decimal