
No.2ベストアンサー
- 回答日時:
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.
ありがとうございます〜
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??
思ってたよりもっと難しい世界がみえちゃいました(´;ω;`)
No.4
- 回答日時:
「あまりがあるならあまり引いて割り切れる状態で」で言いたい意味しだいかなあ...
(N-1)/2 {ただし切り捨て} と
N/2 {ただし切り捨て} が同じなわけはないです。
N が整数であることを前提に、
(N が偶数なら N 奇数なら N-1)/2 と言っているのなら、
N/2 {ただし切り捨て} と同じ値になります。
No.1
- 回答日時:
○ // とは何か?
/ が割り算なのはほぼ確実だとして(それでも小数点以下の処理が言語や状況によって違う)
// は言語によって扱いが違う: 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で割る」のは同じではない。
(浮動小数点数等)
ごめんなさい。パイソンでかんがえてました。そですよね。右ビットシフトでずれたら各桁のオーダーが一つ減って半分になるとおもいました。
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
5%割引の際の、小数点以下の扱...
-
Excel有効数字の丸め方「0捨1入」
-
駐車場賃料の日割り計算について
-
端数処理における「小数以下」...
-
今投稿して解決したのですが 更...
-
負の数の四捨五入の方法
-
数字の下二桁を削除したい
-
勤務の稼働時間の計算がわかり...
-
ACCESSの切り上げ
-
どうしたらいいですか?
-
雇用保険料の計算式について(E...
-
法人税・消費税修正申告納付の仕訳
-
「消費税は別途」の解釈
-
協同組合に支払う会費&賦課金...
-
生産の「時間当たりの出来高」...
-
1個あたりの製産時間を算出した...
-
なぜ消費税10%は×1.1なのですか...
-
消費税の勘定科目は?
-
何が矛盾しているのかわからない
-
高速料金を請求する場合の消費...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
5%割引の際の、小数点以下の扱...
-
勤務の稼働時間の計算がわかり...
-
Excel有効数字の丸め方「0捨1入」
-
Access 1000以下を切り捨てる関数
-
駐車場賃料の日割り計算について
-
次の計算式を使って、 利益率12...
-
端数処理における「小数以下」...
-
COBOLの計算方法
-
雇用保険料の計算式について(E...
-
今投稿して解決したのですが 更...
-
50銭以下切り捨て51銭以上切...
-
VBA
-
数字の double 型 を int型...
-
負の数の四捨五入の方法
-
ACCESSの切り上げ
-
エクセル関数を教えてください
-
SwiftのDouble型の四捨五入につ...
-
右ビットシフト
-
MATLABで小数を整数にするには?
-
計算と小数点の切り捨てを同時...
おすすめ情報