This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Tim Cooke
• Liutauras Vilda
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• fred rosenberger
• salvin francis
Bartenders:
• Piet Souris
• Frits Walraven
• Carey Brown

# some strange results

Greenhorn
Posts: 4

Hi everyone,
I recently posted a topic related with the int UNDERFLOW and OVERFLOW issues. After Reading the responses, I defined a long type value and calculated the determinant, I also put a Integer.MAX_VALUE and Integer.MIN_VALUE in order to avoid the error. The program is about to resolve the well-known Quadratic equation --> ax^2 + bx + c = 0
see My code above

1- d is a long type variable, and is the determinant
2- I checked it for int UNDERFLOW and OVERFLOW
3- If no exceptions were trhown, then a casting is done int calc = (int) d
4- After this an evaluation about the determinant values are carried on (the calc variable)
5- And this is were my problems really started about 2 days ago

The strange thing is: the program is evaluated through JUnit test

Most of them passed, but the (1,0,-2025000000), has been given a totally weird answer and is in this point when despair came

How a determinant can give a value of -489934590 if its formula said (b * b) . (4 * a * c) being a = 1, b = 0, c = -2025000000

and to get the things worse, when I ran the JUnit test in this Ítem, with same values the answer was -45000, 45000

I was about to think JUnit was wrong, but I search on Internet for a Quadratic solution calculator, found one, gave the values (1,0,-2025000000) AND GUESS WHAT? those values -45000, 45000 were shinning on the screen

author
Posts: 23879
142

Julio Moreno wrote:
Most of them passed, but the (1,0,-2025000000), has been given a totally weird answer and is in this point when despair came

How a determinant can give a value of -489934590 if its formula said (b * b) . (4 * a * c) being a = 1, b = 0, c = -2025000000

Well, simply, 4 * 1 * -2025000000 will overflow an integer... which leads me to my question.

Julio Moreno wrote:
1- d is a long type variable, and is the determinant
2- I checked it for int UNDERFLOW and OVERFLOW

Can you explain how your check for overflow and underflow works? ... because I don't see how it can work.

Henry

Marshal
Posts: 69889
278
I was always taught that underflow is a problem specific to floating‑point arithmetic, and both the kinds of error you are looking for are called overflow. You have already discussed overflow in another thread.
As Henry says, it is impossible for d (bad name for a variable) to be outwith the range of an int, so your tests for overflow will always fail.

Julio Moreno
Greenhorn
Posts: 4
Thanks for the answer gave to my post. Some observations: the determinant d is a long type variable, with a storage capacity bigger than an int type.

The validation for overflow and underflow is carried out right after d is calculated by comparing its value with Integer.MAX_VALUE and Integer.MIN_VALUE

I am still confused

Any ideas?

Henry Wong
author
Posts: 23879
142

Julio Moreno wrote:
The validation for overflow and underflow is carried out right after d is calculated by comparing its value with Integer.MAX_VALUE and Integer.MIN_VALUE

Due to precedence, of mostly based on the parenthesis in the expression, the actual cast (conversion) of the integer result to a long value, and subsequent assignment to the d variable, completes after the integer expression has already overflowed.

This means that what the d variable holds is the already overflowed, and still integer resultant value. It will *never* have a value more than max int or less than min int.

Henry

Bartender
Posts: 732
10

This cannot be used to detect overflow of integer, since b*b (which results in an int( might itself overflow,
You would need to check each operation:

Here, all of the interim values will be longs.

Underflow has no meaning when working with integers (getting a values less than MIN_VALUE is still an overflow).
Undeflow is used with real numbers, meaning that the value is so close to zero that it cannot be distinguished from zero.

Campbell Ritchie
Marshal
Posts: 69889
278
Are you sure you need all those casts? Casting the first b and changing the 4 to 4L shou‍ld be sufficient. As you imply, the cast in the first bit of code will not do anything, because of the ()s.
I presume the . after 4 is a misprint, otherwise it will become a double and the whole expression will eventually be cast to a double. A double has less pecision than a long, as mentioned in this thread. Please have a look at that link.

Are there several people doing the same exercise in the same place? This is the third thread about overflow problems with quadratic equations.

Fred Kleinschmidt
Bartender
Posts: 732
10
Yes, you are right. Each subexpression only needs one cast to long, and the period after the 4 is a typo. I am just so used to seeing the variable "d" to denote an interim double that my automatic typing took over.

Campbell Ritchie
Marshal
Posts: 69889
278

Fred Kleinschmidt wrote:. . . . I am just so used to seeing the variable "d" to denote an interim double that my automatic typing took over.

Just goes to show how important good variable names are.

 If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad! Thread Boost feature https://coderanch.com/t/674455/Thread-Boost-feature