Variablenüberlauf bei Addition, Multiplikation

Status
Nicht offen für weitere Antworten.

2k7

Neues Mitglied
Hallo!
kann mir jemand erklären was passiert, wenn man 2 variablen mit dem typ int z.B. addiert oder multipliziert und dass ergebnis größer ist als der wertebereich vom typ int??? das richtige ergebnis kommt jedenfalls nicht zustande, was mich auch nicht wundert.
wie wird dass in java gerechnet? was geschieht im background?

gruß,
2k7
 

Wildcard

Top Contributor
Die Zahlen sind im Zweierkomplement dargestellt. Wenn der positive Wertebereich überläuft geht es im negativen weiter.
JLS hat gesagt.:
The binary * operator performs multiplication, producing the product of its operands. Multiplication is a commutative operation if the operand expressions have no side effects. While integer multiplication is associative when the operands are all of the same type, floating-point multiplication is not associative.

If an integer multiplication overflows, then the result is the low-order bits of the mathematical product as represented in some sufficiently large two's-complement format. As a result, if overflow occurs, then the sign of the result may not be the same as the sign of the mathematical product of the two operand values.

The result of a floating-point multiplication is governed by the rules of IEEE 754 arithmetic:

* If either operand is NaN, the result is NaN.
* If the result is not NaN, the sign of the result is positive if both operands have the same sign, and negative if the operands have different signs.
* Multiplication of an infinity by a zero results in NaN.
* Multiplication of an infinity by a finite value results in a signed infinity. The sign is determined by the rule stated above.
* In the remaining cases, where neither an infinity nor NaN is involved, the exact mathematical product is computed. A floating-point value set is then chosen:
o If the multiplication expression is FP-strict (§15.4):
+ If the type of the multiplication expression is float, then the float value set must be chosen.
+ If the type of the multiplication expression is double, then the double value set must be chosen.
o If the multiplication expression is not FP-strict:
+ If the type of the multiplication expression is float, then either the float value set or the float-extended-exponent value set may be chosen, at the whim of the implementation.
+ If the type of the multiplication expression is double, then either the double value set or the double-extended-exponent value set may be chosen, at the whim of the implementation.

Next, a value must be chosen from the chosen value set to represent the product. If the magnitude of the product is too large to represent, we say the operation overflows; the result is then an infinity of appropriate sign. Otherwise, the product is rounded to the nearest value in the chosen value set using IEEE 754 round-to-nearest mode. The Java programming language requires support of gradual underflow as defined by IEEE 754 (§4.2.4).

Despite the fact that overflow, underflow, or loss of information may occur, evaluation of a multiplication operator * never throws a run-time exception.
 

mlux

Mitglied
http://www.unix.org.ua/orelly/java/langref/ch04_05.htm

If the multiplication of integer data overflows, the low order bits of the product are returned; no exception is thrown. The most significant bit of the low order bits is treated as a sign bit. When overflow occurs, the sign of the number produced may not be the same as the sign of the mathematically correct product, due to the limitations of the two's complement representation used for integer data.

recht interessant - wusste ich auch noch nicht.[/quote]

EDIT: happy crossposting :)
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
J Variablenüberlauf Java Basics - Anfänger-Themen 12
volcanos Addition -> List<Integer> mit Arrays.asList() versus List<Integer>ArrayList<>() Java Basics - Anfänger-Themen 14
K Präzedenregeln in Java sagen, dass +expr und -expr vor + von Addition und - von Addition stehen, warum wird dann z. B. a+b als eine Addition ausgeführ Java Basics - Anfänger-Themen 7
J Double Variable und Addition Java Basics - Anfänger-Themen 2
JavaClap "Bruchrechner" liefert Fehler/keine Ausgabe bei Addition und Subtraktion Java Basics - Anfänger-Themen 0
G Java Bruchrechner Addition, Multiplikation... Java Basics - Anfänger-Themen 12
L Addition von Arrays über die Parameterliste Java Basics - Anfänger-Themen 11
Z Matrix Klasse mit Mehrdimensionalen Array (Addition, Multiplikation, to String) Java Basics - Anfänger-Themen 57
L Addition von 1 bis n, in Arrays umstellen Java Basics - Anfänger-Themen 15
E Addition Array Java Basics - Anfänger-Themen 6
M Array Addition Binärzahlen Java Basics - Anfänger-Themen 9
A Binäre Addition Java Basics - Anfänger-Themen 15
M Addition mit hohen Zahlen - Hilfe Java Basics - Anfänger-Themen 5
N Rekursive Addition mit Scanner Java Basics - Anfänger-Themen 12
X Compiler-Fehler Possible loss of precision bei addition von int und char Java Basics - Anfänger-Themen 5
C Logischer Fehler mit Addition von Arrays Java Basics - Anfänger-Themen 3
I Schriftliche Addition Arrays Java Basics - Anfänger-Themen 2
T Operatoren Multiplikation nur mit Addition, Subtraktion und Vergleich Java Basics - Anfänger-Themen 29
B Erste Schritte Addition von double in switch-Anweisung Java Basics - Anfänger-Themen 2
X Addition von Strings Java Basics - Anfänger-Themen 12
X Arrays/Addition Java Basics - Anfänger-Themen 18
R addition von 2 Array Objekten Java Basics - Anfänger-Themen 5
S Multiplikation durch Addition, Subtraktion und Vergleich von Zahlen Java Basics - Anfänger-Themen 14
K Datentypen Kurzform Addition geht, Langform scheitert am Typen Java Basics - Anfänger-Themen 6
A Binäre Addition Java Basics - Anfänger-Themen 5
K char-int-Addition in System.out.print Java Basics - Anfänger-Themen 3
T Matrizzen addition mit zufallszahlen Java Basics - Anfänger-Themen 10
P Addition von 2 Variablen Java Basics - Anfänger-Themen 3
J Vector Addition Java Basics - Anfänger-Themen 5
E Addition von char und int als Zeichen ausgeben Java Basics - Anfänger-Themen 11
J Addition zweier Zahlen Java Basics - Anfänger-Themen 9
C Falsches Ergebnis float-Addition Java Basics - Anfänger-Themen 10
X Addition rekursiv ohne Schleife Java Basics - Anfänger-Themen 10
S Problem bei Matrix Addition Java Basics - Anfänger-Themen 5
E Addition von Arrays Java Basics - Anfänger-Themen 4
H Mysteriöse Ausgaben nach Addition Java Basics - Anfänger-Themen 5
S Addition zweier 32 Byte langer ByteArrays Java Basics - Anfänger-Themen 5
M Ausgabe einer gerade Zahl nur mit Addition,subtraktion und vergleichsoperatoren! Java Basics - Anfänger-Themen 4
J klasse vektor addition Java Basics - Anfänger-Themen 10
S Anzahl der Überträge bei einer Addition ausgeben Java Basics - Anfänger-Themen 8
S Addition von eigenen Objekten mit "+" Symbol Java Basics - Anfänger-Themen 19
A ungenaue addition Java Basics - Anfänger-Themen 2
A Addition mehrdimmensionaler Arrays - Klausurfrage :-( Java Basics - Anfänger-Themen 3
G Addition von 2 Matrizen Java Basics - Anfänger-Themen 31
J Addition von double values fehlerhaft? Java Basics - Anfänger-Themen 5
H Subtraktion mittels Negierung auf Addition zurückführen Java Basics - Anfänger-Themen 4
W Addition von Arrays bzw. Vektoren Java Basics - Anfänger-Themen 2
V Addition in einem Zweidimensionalenfeld Java Basics - Anfänger-Themen 8
L Switch-Case Multiplikation wird nicht ausgegeben Java Basics - Anfänger-Themen 1
padde479 Array Multiplikation der ersten n Zahlen Java Basics - Anfänger-Themen 7
S Multiplikation von zwei Labels Java Basics - Anfänger-Themen 7
F Matrix Multiplikation Java Basics - Anfänger-Themen 3
O Erstes Programm: Matrizen Multiplikation Java Basics - Anfänger-Themen 10
H Variablen Multiplikation einer inkrementierten Variablen Java Basics - Anfänger-Themen 5
D Methoden Matrix Multiplikation Java Basics - Anfänger-Themen 27
J Funktionsaufrufe Multiplikation von Arrays Java Basics - Anfänger-Themen 3
Joker4632 Klassen BigDecimal Multiplikation liefert nicht erwarteten Wert Java Basics - Anfänger-Themen 6
TomatenBrot447 Adapter für eine optimierte Multiplikation Java Basics - Anfänger-Themen 5
U Ist diese Methode zur Matrix Vektor Multiplikation korrekt ? Java Basics - Anfänger-Themen 5
K Multiplikation zweier Matrizen Java Basics - Anfänger-Themen 23
P Variablen Negatives Ergebnis bei Multiplikation von großen Zahlen Java Basics - Anfänger-Themen 2
R Matrix-Vektor-Multiplikation Java Basics - Anfänger-Themen 13
K Seltsames Ergebnis in Netbeans bei einfacher Multiplikation Java Basics - Anfänger-Themen 5
V Vor jeder Multiplikation den linken Multiplikator abrunden Java Basics - Anfänger-Themen 4
M Matrix Matrix Multiplikation Java Basics - Anfänger-Themen 6
W Multiplikation mit rekursivem Aufruf Java Basics - Anfänger-Themen 17
S Multiplikation von großen Zahlen, ohne BigInt uä Java Basics - Anfänger-Themen 7
F.S.WhiTeY Multiplikation nach der Schulmethode Java Basics - Anfänger-Themen 6
K Matrizen Multiplikation Java Basics - Anfänger-Themen 3
B Multiplikation bei Generics Java Basics - Anfänger-Themen 9
G Multiplikation falsches Ergebnis Java Basics - Anfänger-Themen 5
K double Multiplikation Java Basics - Anfänger-Themen 2
K einfache Multiplikation Java Basics - Anfänger-Themen 6
O Überlauf durch Multiplikation Java Basics - Anfänger-Themen 7

Ähnliche Java Themen

Neue Themen


Oben