File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Large number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Large number" Watch "Large number" New topic
Author

Large number

Tom Schuman
Greenhorn

Joined: Dec 30, 2004
Posts: 18


When i give the program these arguments: 0 17e307
The output is a large number

but when i give it these arguments : 0 18e207
The output is ?

Where does it turn to a question mark?

Is it when it makes the String a Double or is it somewhere in DecimalFormat
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
I tried it with 1.4.1. and 1.5.0 with large number output for both

C:\_Work\java>javac FPAdder.java

C:\_Work\java>java FPAdder 0 17e307
170,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000

C:\_Work\java>java FPAdder 0 18e207
18,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000

C:\_Work\java>java -version
java version "1.4.1_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_05-b01)
Java HotSpot(TM) Client VM (build 1.4.1_05-b01, mixed mode)
[ February 25, 2005: Message edited by: Carol Enderlin ]
Mary Anderson
Greenhorn

Joined: Feb 25, 2005
Posts: 6
sorry for interfiering to your topic
but is actually the common way to add a number in the total sum?
sorry but I saw that it is around the topic that i'm reading now
and i thought to place my questions
thank you again
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Same here (with 1.5)...

H:\Java>java FPAdder 0 17e307
170,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000

H:\Java>java FPAdder 0 18e207
18,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,
000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000

H:\Java>java -version
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode)
[ February 25, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Mary Anderson:
...is actually the common way to add a number in the total sum? ...

Yes, it's common to store a cumulative value in a loop function like this. With each iteration of the loop, that value (in this case, sum) gets updated.

Or are you asking about the combined operators for assignment and addition?
Tom Schuman
Greenhorn

Joined: Dec 30, 2004
Posts: 18
So do you mean that that number doesn't fit into String? I tought String was unlimited size.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Tom Schuman:
...when i give it these arguments : 0 18e207
The output is ? ...

I think the confusion here is from a typo: Did you mean to say "18e307"?

This would make sense, because the maximum value for a double is approx. 1.798e308. If you enter anything above this, the double value will be "Infinity." And if you try to format that with DecimalFormat, you'll get "?".

So 17e307 is okay (because 1.7e308 < 1.798e308), but 18e307 is not (because 1.8e308 > 1.798e308).

(Note: Print out "sum" without formatting to see the Infinity value.)
[ February 25, 2005: Message edited by: marc weber ]
Tom Schuman
Greenhorn

Joined: Dec 30, 2004
Posts: 18
Yes i meant to say 18e307

I printed the sum and the output was infinity.

Shouldn't there be an error that the value is out of range like int and long has?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

See the following for details on floating point numbers...

http://www.public.iastate.edu/~sarita/ieee754/homepage.html
Saravanan Jothimani
Greenhorn

Joined: Feb 25, 2005
Posts: 26
import java.text.DecimalFormat;
public class FPAdder
{
public static void main(String[] args)
{
int numArgs = args.length; //this program requires at least two arguments on the command line
if (numArgs < 2)
{
System.out.println("This program requires two command-line arguments.");
}
else
{
double sum = 0.0;
for (int i = 0; i < numArgs; i++)
{
sum += Double.valueOf(args[i]).doubleValue();
}
//format the sum
DecimalFormat myFormatter = new DecimalFormat("###,###.##");
String output = myFormatter.format(sum);
//print the sum
System.out.println(output);
}
}
}
Double.valueOf(args[i]).doubleValue();

can anybody explain me about this line
I want to know whether string valueOf is converted to doubleValue() or any other way.

Thanks in Advance

Saravanan
Liam Tiarnach
Ranch Hand

Joined: Aug 06, 2004
Posts: 51
Double.valueOf(args[i]).doubleValue();

can anybody explain me about this line
I want to know whether string valueOf is converted to doubleValue() or any other way.

Thanks in Advance

Saravanan[/QB]
If you read the API for java.lang.Double class, you will see that Double.valueOf(string) will return a new Double containing that double value parsed from the String, the the .doubleValue() method is called on the new Double to return it's value as a primative double...
in essence that line is the same as...


hope this helps...


- Liam...<br />- ' He who never sleeps... '
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Saravanan Jothimani:
... Double.valueOf(args[ i ]).doubleValue();

can anybody explain me about this line? I want to know whether string valueOf is converted to doubleValue() or any other way...

As explained by Liam above, Double.valueOf(args[ i ]) is returning a new object of type Double. Then the method doubleValue() is called on that instance of Double to return a primitive double.

However, this could also be accomplished using the static method Double.parseDouble(args[ i ]), which returns a primitive double without the intermediate (and unnecessary) step of creating a new Double object.
[ February 26, 2005: Message edited by: marc weber ]
Saravanan Jothimani
Greenhorn

Joined: Feb 25, 2005
Posts: 26
Thanks Liam Tiarnach & marc weber
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Large number
 
Similar Threads
Sombody please help this Noob!
Logical error in my Program
Float
Out of bounds Exception
Project Euler : Problem 23