• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Large number

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So do you mean that that number doesn't fit into String? I tought String was unlimited size.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the following for details on floating point numbers...

http://www.public.iastate.edu/~sarita/ieee754/homepage.html
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Liam Tiarnach & marc weber
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic