Skip to content

Conversation

@Fuminiton
Copy link
Owner

> >>> round(0.1, 1) + round(0.1, 1) + round(0.1, 1) == round(0.3, 1)
> False
> >>> math.isclose(0.1 + 0.1 + 0.1, 0.3)
> False
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここ転記ミスってます。Trueが正しいです。

Comment on lines +186 to +187
- bin32なら符号1bit、指数部8bits、仮数部23bits。単精度とも言われるもの。
- bin64なら符号1bit、指数部11bits、仮数部52bits。倍精度とも言われるもの。
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この桁数覚えておいてもいいでしょう。
nan, inf についても見ておいてください。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントありがとうございます。
どちらも指数部が全て1になるんですね。

## step3
```py
class Solution:
def _get_pow(self, base: float, exp: int) -> float:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これ、関数内関数のほうが良さそうに見えます。
n < 0 を入れられると間違った値をだしてしまうので、myPow経由で呼んでほしくなりました。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご指摘のとおりで、_get_powはnが0以上の整数であることが前提になっていました。で、関数内関数ならこの前提を強制できますね。

また、関数内関数にすることで失われる_get_powの再利用性も、そもそもmyPow自体を呼び出せばいい話ですね。

ネストが減っていいだろうくらいでやりましたが、色々考えることがあることに気付けました。ありがとうございます。


### 感想
浮動小数点周りはSWEの入り口として知っておくべきことが多そう。
あと、一旦cを読めるように学習しないと、cpythonの読解の質が上がらなそう。 No newline at end of file
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同感です。私もAI頼りに読んでいて、詳細まで読めていないです。

raise ZeroDivisionError("When n < 0, x must be non-zero")
if x == 0:
if n == 0:
return 1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typing で float を返すと書いてあるにもかかわらず、 int を返している点に、違和感を感じました。好みの問題かもしれません。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントありがとうございます。
自分も違和感を感じ、step2からは1.0を返すようにしました。

```py
class Solution:
def myPow(self, x: float, n: int) -> float:
def get_pow(x: float, n: int) -> float:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get という英単語は、既に存在しているものを取得するというニュアンスがあるように思います。 calculate_pow() はいかがでしょうか?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

レビューありがとうございます。

get という英単語は、既に存在しているものを取得するというニュアンスがあるように思います。

これ意識できておらず、何でもかんでもget使っていました。calculateいいですね。

half_pow_squared = half_pow * half_pow
if exp % 2 == 1:
return base * half_pow_squared
return half_pow_squared
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exp が偶数であれば末尾再帰さいてきかがかかるように書けそうです。

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

Successfully merging this pull request may close these issues.

6 participants