アプリ版:「スタンプのみでお礼する」機能のリリースについて

1ひいてから(つまりあまりがあるならあまり引いて割り切れる状態で)//2

N/2 の小数点以下を切り捨てた値

は同値ですか?

A 回答 (5件)

Superficially speaking, it depends on the specification of a programming language.


Technically speaking, as I assume this is what you really want to know, NO.
For instance, right-shifting 9 a bit may be recognized as an integer type, whereas flooring 9 / 2 may be recognized as a float type in most cases.
Does an integer type equal to a float type? They are DIFFERENT; however, generally speaking, we want to treat both integers and floating point numbers as just numbers; therefore a specification of a programming language defines to treat so on calculation for its convenience, or user-friendliness.
On the other hand, for anti-example, there is a stroooooongly typed language like OCaml. This heck does not allow us to do addition/subtraction/multiplication/division between integers and floating point numbers.

# 1 + 2.0;;
Error: This expression has type float but an expression was expected of type
    int

Thus, the language like OCaml easily says NO to what you've asked.

# Int.shift_right 9 1 == Float.floor(9.0 /. 2.0);;
Error: This expression has type float but an expression was expected of type
    int

Now you know, at the high-level programming language layer, "it depends".

In addition to that, by the way, some modern languages have fraction now. If you love to use them, you would not be annoyed with sickly flooooooating point numbers any more.

;; Racket(A Lisp implementation: https://racket-lang.org)
> (/ 9 2)
4 1/2
> (floor (/ 9 2))
4
>

# Python
>>> from fractions import Fraction
>>> from math import floor
>>> Fraction(9, 2)
Fraction(9, 2)
>>> floor(Fraction(9, 2))
4


※: You may know what "type" is, but here let it be cleared more.
A Type is basically a TAG to encode/decode something in binary level.
Everything we want to "express" is encoded into binaries and put in memories, with a certain rule. Everything in memories is decoded into what we are familiar to on the monitor.
Now, how to put "what we want to express" as binaries, or data, heavily relies on "rules", with clues to know "what is what". In order to do this, there are types; in other words, there are different rules to put binaries for integers and floating point numbers.
You had better read this:

IEEE 754:
https://en.wikipedia.org/wiki/IEEE_754

To put "floating point numbers" in memories is much more complex than to do integers. Headache. Give me a break.
Anyway, at the very lowest level, we cannot say integers, even though it is made by right shift, are equal to floating point numbers.
    • good
    • 0
この回答へのお礼

助かりました

ありがとうございます〜
i just skimmed through the page but I'm still trying to wrap my head around this. I mean why do they have so much variance in just processing numbers?? Can't we unify all this, mess ?? please??
思ってたよりもっと難しい世界がみえちゃいました(´;ω;`)

お礼日時:2024/05/11 22:26

ああ、質問のタイトルは「右ビットシフト」だった。


右ビットシフトは、
N/2 {ただし切り捨て} と同じ値になりますね。

「あまりがあるならあまり引いて割り切れる状態で」については
No.4 に書いたとおり。
    • good
    • 0
この回答へのお礼

助かりました

たしかにそですね?ありがとうございます

お礼日時:2024/05/11 22:27

「あまりがあるならあまり引いて割り切れる状態で」で言いたい意味しだいかなあ...


(N-1)/2 {ただし切り捨て} と
N/2 {ただし切り捨て} が同じなわけはないです。

N が整数であることを前提に、
(N が偶数なら N 奇数なら N-1)/2 と言っているのなら、
N/2 {ただし切り捨て} と同じ値になります。
    • good
    • 0

Nが正なら同じ。


プログラミングで2で割る時、1ビット右へロジカルシフトしますから。
但し、元がo以上の整数の場合のみです。
    • good
    • 0
この回答へのお礼

ありがとう

ありがとうございます

お礼日時:2024/05/11 22:27

○ // とは何か?


/ が割り算なのはほぼ確実だとして(それでも小数点以下の処理が言語や状況によって違う)
// は言語によって扱いが違う: C言語に近い文法を使うものでは、 // は行末までのコメントになる。

○ Pythonだとして、これくらいのことなら人に聞く前に自分で実際に色々試してみればいい

切捨: math.floor
https://docs.python.org/ja/3.12/library/math.htm …

import math
N=4 #いろいろ変えてみる
print( (N-1) // 2, N / 2, math.floor(N / 2) )


○ そもそも「1ひく」ことと「あまりがあるならあまり引いて割り切れる状態にする」ことは同じではない。

○ついでに「右ビットシフト」と「2で割る」のは同じではない。
(浮動小数点数等)
    • good
    • 0
この回答へのお礼

ありがとう

ごめんなさい。パイソンでかんがえてました。そですよね。右ビットシフトでずれたら各桁のオーダーが一つ減って半分になるとおもいました。

お礼日時:2024/05/11 22:09

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

このQ&Aを見た人はこんなQ&Aも見ています