aspose file tools*
The moose likes Java in General and the fly likes Double and Float Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Double and Float " Watch "Double and Float " New topic
Author

Double and Float

Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
Can anyone throw light on this.



Thanks
user101
Problems And Solutions - Algorithms
Ankur Kapoor
Greenhorn

Joined: Sep 13, 2006
Posts: 11

Decimal precisions are always an issue with Java's float and double, that's the reason Java introduced BigDecimal to avoid such dependencies.


Ankur Kapoor

Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
So is there no other way than using BigDecimal
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

That depends on what your requirements are. If the lack of exact accuracy isn't a problem, and all you need is the output to look a bit more predictable, you can use DecimalFormat to produce output to the precision you want.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Or String.format, and use System.out.printf for printing.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
I don't want it for printing purpose.I want to use the calculated value

In first case value which i want is 845
and in second case value i need is 8178

Currently in my code i am calculating both values
using Math.max(x,y) i am selecting the value.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

Satyajit Bhadange wrote:I don't want it for printing purpose.I want to use the calculated value

In first case value which i want is 845
and in second case value i need is 8178

Currently in my code i am calculating both values
using Math.max(x,y) i am selecting the value.

You can't be sure that will always work. You are likely at some point to get values like

845.0 845.00000000021

You've been told both what the problem and solution are. If you need that degree of precision, you can't use floats/doubles.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
So it seems i need to stick to BigDecimal Only
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

You sound like you think that is a bad thing. Writing code is like building a house. You need to use the right tool for the job. It seems like you're saying "I want to drive in this nail. Can I use a wrench".

you're told to use a hammer.

You the reply with "well, I can use this really, really big wrench".

If BigDecimal is the right tool, why don't you want to use it?
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
Its not like that,I just wanted to make sure.

I used BigDecimal in my code but still facing same problem.

Below Code is using BigDecimal




Below code i am using Math.max(double,float)


The problem statement is from Codeforces Problem Statement - Codeforces

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
If you pass a double to the constructor of a BigDecimal, you will get the imprecision of double, recorded very accurately. If you want 1.2345, you need to pass the String "1.2345" to the BigDecimal constructor.
campbell@computer:~/java> java BigDecimalDemo 1.0
bd1 equals bd2: true
bd1 = 1.000000000000000000000000000000, bd2 = 1.000000000000000000000000000000
campbell@computerj:~/java> java BigDecimalDemo 0.1
bd1 equals bd2: . . .
Try it for yourself.
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
Okay.

I got it now.
It was good experience to learn about about decimal, float and BigDecimal today.

Thanks Everyone for their replies.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Well done working it out

or . . . you're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Double and Float