• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Double and Float

 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone throw light on this.

 
Ankur Kapoor
Greenhorn
Posts: 11
Java Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Decimal precisions are always an issue with Java's float and double, that's the reason Java introduced BigDecimal to avoid such dependencies.
 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So is there no other way than using BigDecimal
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or String.format, and use System.out.printf for printing.
 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it seems i need to stick to BigDecimal Only
 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done working it out

or . . . you're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic