GeeCON Prague 2014*
The moose likes Cattle Drive and the fly likes Arithmetic Overflow in exercise 4b Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Arithmetic Overflow in exercise 4b" Watch "Arithmetic Overflow in exercise 4b" New topic
Author

Arithmetic Overflow in exercise 4b

Eviano Afiemo
Greenhorn

Joined: Mar 20, 2001
Posts: 21
Hi guys...I need some help with exercise 4b.
Okay people, without giving too much away I'm trying to determine the number of
billions in a given number e.g. 555,444,333,222 .
To achieve this I need to divide it by 1000,000,000,000.
However, my compiler gives me the following error " Literal overflows capacity of int type ...":
The only way of performing such a calculation is by appending L to the value to indicate
that it is of type long.
i.e 555444333222L / 1000000000000L .

However since the number will actually be a variable, I somehow need to pass it into methods as a an argument without the expression above falling flat on its face.

I've almost completed 4b, and that's the remaining obstacle as I see it.

thanks in advance ...
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
If I am correct, the problem is with the math you are trying to accomplish. Look again at how much you are dividing by and what you are trying to seperate out.
Jason
Daniel Dunleavy
Ranch Hand

Joined: Mar 13, 2001
Posts: 276
You might consider handling it in string chunks
Dan
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
Originally posted by Eviano Afiemo:

However, my compiler gives me the following error " Literal overflows capacity of int type ...":
The only way of performing such a calculation is by appending L to the value to indicate
that it is of type long.
i.e 555444333222L / 1000000000000L .

However since the number will actually be a variable, I somehow need to pass it into methods as a an argument without the expression above falling flat on its face.

Why not make the variable a long instead of an int.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Originally posted by Eviano Afiemo:
... type long.
i.e 555444333222L / 1000000000000L .

However since the number will actually be a variable, I somehow need to pass it into methods as a an argument without the expression above falling flat on its face.

long number = 555444333222L / 1000000000000L
??


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Eviano Afiemo
Greenhorn

Joined: Mar 20, 2001
Posts: 21
Thanks for your replies ....


  • Jason I see what you're saying about the math in the example I gave, I mistakenly added an extra zero, but that's not the cause of the problem (I wish it were though :-)).
  • [list]Richard, even when I declared the variables as type long I still got the same error.
    i.e when I did this:

    the error goes something like this :
    integer number too large: 123456789012
    long L1 = 123456789012 ;

    ^
    [/list]

  • long number = 555444333222L / 1000000000000L
    ??

    Marilyn I read in a book, that Java defaults to integers for number literals, and the only way to let the compiler know that you're dealing with a long is by appending an "L" to the end of the number. That's why the above code works.

  • I just wanted to know if anyone else had come across this problem. I find it so frustrating. I'll probably have to go with Dan's suggetion (breaking the number in to chunks). But still, I'd like to know how one would deal with the problem.

    that's all for now. Thanks people

    [This message has been edited by Eviano Afiemo (edited July 24, 2001).]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Originally posted by Eviano Afiemo:
Marilyn I read in a book, that Java defaults to integers for number literals, and the only way to let the compiler know that you're dealing with a long is by appending an "L" to the end of the number. That's why the above code works.
[/list]

Yes, that is correct. Java defaults to integers for number literals. Try this one:

I'll probably have to go with Dan's suggetion (breaking the number in to chunks).

This is a better approach for this assignment.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
By the way, do you know what the possible range of values is for an int?
Eviano Afiemo
Greenhorn

Joined: Mar 20, 2001
Posts: 21
Originally posted by Marilyn deQueiroz:
By the way, do you know what the possible range of values is for an int?

Yes Marilyn, it's [ -2,147,483,648 - 2,147,483,647 ] ( at least that what it says in "Java Software Solutions by John Lewis and William Loftus" ) .
Thanks for your help, I've started over with my program, using the other approach.
Eviano Afiemo
Greenhorn

Joined: Mar 20, 2001
Posts: 21
Although I've started over, I've also managed to solve the problem on yhe basis of my old approach. I'm now going to submit it like this, to see if I can learn anything from the nitpicks which I'm very sure I'll recieve.
Once again , thanks everybody.
One thing I can say is that it feels good when you solve a problem that's been bugging you for a while. By coincidence one of my JSP projects also fell into place today... It might have something to do with the good weather we've been having recently in England.
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
It doesn't feel good, it feels grrrrrrreat!
This one took me something like 13 tries to get right, so don't feel bad if your solution gets shot down several times, I think everyone learned a great deal from this assignment.
 
GeeCON Prague 2014
 
subject: Arithmetic Overflow in exercise 4b