C++'ā integral tipi, kas mazāki (bitu garumā) par int automātiski kāstojas uz int, pirms izdarīt aritmētiskas izteikmes (+ - * /). Respektīvi 1-ais variants būs ekvivalents 3-šajam. Tas attiecas arī uz bool, char, whcar_t saskaitīšanu, kā arī uz unsigned char/short.
2-ais gan izpilda pilnīgi savādāku formulu nekā 3-šais.
Matemātiski to varētu pierakstīt kā floor(a/2.0)+floor(b/2.0) atšķirībā no floor((a+b)/2.0). Jo integeru dalīšana apaļojas uz leju.
Par C gan nezināšu.
Quotes iz C++ standarta:
Expressions -
http://www.kuzbass.ru:8086/docs/isocpp/expr.html-9- Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:
[..]
* otherwise, the integral promotions (conv.prom) shall be performed on both operands.*
Integral promotions -
http://www.kuzbass.ru:8086/docs/isocpp/conv.html#conv.prom-1- An rvalue of type char, signed char, unsigned char, short int, or unsigned short int can be converted to an rvalue of type int [..]
RTFM :)
(Lasīt komentārus)
Nopūsties: