while loop and double acting weirdly - java

Trying to solve a differential equation numerically (for the first time). My program asks for a bunch of parameters and then calculates the amount of time required to chill a bottle of wine to a given temperature. This is my main:
import java.util.Scanner;
public class Step2_lab11 {
public static int TAO = 50;
public static double DELTA_MINUTES = 0.1;
public static void main(String[] args) {
System.out.println("VINETS AVKYLNINGSTID \n");
System.out.println("Ange vinets temperatur:");
Scanner userIn = new Scanner(System.in);
double wineTemp = userIn.nextDouble();
System.out.println("Vinets önskade temperatur:");
double preferredTemp = userIn.nextDouble();
System.out.println("Kylens/frysens temperatur:");
double chillTemp = userIn.nextDouble();
WineChiller wineChiller = new WineChiller();
double elapsedTime = 0.0;
while(wineTemp > preferredTemp) {
elapsedTime = elapsedTime + DELTA_MINUTES;
double dT = wineChiller.getChillingTime(TAO, DELTA_MINUTES, chillTemp, preferredTemp, wineTemp);
wineTemp = wineTemp - dT;
System.out.println(elapsedTime);
}
}
}
And this is the WineChiller.java file:
public class WineChiller {
public WineChiller() {
}
public double getChillingTime(int TAO, double DELTA_MINUTES, double chillTemp, double preferredTemp, double wineTemp) {
double dT = (wineTemp - chillTemp) * DELTA_MINUTES / TAO;
return dT;
}
}
The Syso part of the while loop produces this (with wineTemp = 25, preferredTemp = 16, chillTemp = 5)
0.1
0.2
0.30000000000000004
....
29.300000000000146
29.400000000000148
29.50000000000015
29.60000000000015
29.700000000000152
29.800000000000153
29.900000000000155
No idea why it adds the random decimals. I also think (but I'm not 100%) the right answer should be exactly 30 minutes, not 29.9. Am I missing some obvious logic error here?

You need to read this.
This is just how binary numbers and IEEE floating point representation work.
You can no more represent 0.1 exactly in binary than you can 1/3 in decimal.
This is why you should not compare values when you use double or float; you need a tolerance for absolute value of differences.
Looks like a simple Euler integration of the first order ODE for transient heat transfer for a lumped mass. Make sure that you understand how time step choice affects stability and accuracy.

Related

How to get greater precision for PI using the Harmonic Series

trying to get back into Java and have decided to tackle PI. So I made this program based on the Harmonic Series:
public static void main(String [] args)
{
double denominator = 1.0;
double tempValue;
double PI = 0.0;
// End point for program
double stopPoint = 1234.5;
for( int i = 1; i < stopPoint; i++ )
{
tempValue = Math.sqrt( (1/(denominator*denominator))/6 );
PI = PI + tempValue;
denominator = denominator + 1.0;
}
System.out.println( "PI = " + PI );
The application prints this:
PI = 3.1417306496998294
So you can see its mildly working. But when I change the stopPoint value any more I'm not getting a change in precision, at all.
For example changing it to 1234.75 gives the same answer - or perhaps print can't display the exact value? If so what is the best way to print out values like these?
Thanks
EDIT
I've added this code as its a change to the code posted above. Some of the changes include the use of Big Decimal and the inclusion of a while loop instead of a for.
import java.math.BigDecimal;
import java.math.MathContext;
public class MyPI
{
final static BigDecimal ONE = new BigDecimal(1);
final static BigDecimal SIX = new BigDecimal(6);
public static void main(String [] args)
{
BigDecimal deno, temp, tempPI;
int start, end;
start = 1;
end = 500000;
temp = new BigDecimal(0);
// Starting denominator point
deno = ONE;
while( start < end )
{
// Without precision and rounding mode, it will try to return a
// never ending number
temp = temp.add( ONE.divide(deno.pow(2),MathContext.DECIMAL64) );
deno = deno.add(ONE);
start = start + 1;
}
tempPI = temp.multiply(SIX);
// Need to convert to double for square root
double PI = Math.sqrt( tempPI.doubleValue() );
System.out.println( "PI: " + PI );
}
}
This produces the following result:
PI: 3.1415907437318054
Thanks all for the help - will probably add a timer to track how long it takes to do this.
I've been using the BigDecimal type instead of the double, but I have hit a bit of a roadblock—the square root.
Don't take the square root of each term. As shown in this example, add the terms of the series, which has the exact sum π2/6. When your loop terminates, multiply by six and then take a single square root.
If you want more precision, you can use Java's BigDecimal.
Use Java BigDecimal instead of Double that has a limited precision.
The BigDecimal class can give you "arbitrary-precision signed decimal numbers", which is what you want in this case, although the instances of BigDecimal are a little more tricky to work with than literals, the class actually works quickly and can be used to do what you need fairly accurately.
Just for your information, though, using the harmonic series to calculate Pi is pretty inefficient, but I understand doing it as a fun program or to learn something new.

Truncating Double to 2 decimal places?

I know this question is repeated alot, but please have a look at the statement first than mark it already answered :)
For truncating the double values 2 decimal places I use two ways which are mostly mentioned everywhere. They are given below
//this one
DecimalFormat dtime = new DecimalFormat("#.##");
return Double.valueOf(dtime.format(val));
//or the one below
BigDecimal bd = new BigDecimal(val);
BigDecimal rounded = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
return rounded.doubleValue();
The problem is that for both the ways I mostly get correct rounded values in the dataset. But strangely at the same time I get values like 2.00000000000005 or 19.97999999999.
The problem that I dont get is that why only a few values are not rounded of. What could be wrong?
For truncating the double values 2 decimal places I use two ways which are mostly mentioned everywhere.
And they are both wrong, because they are attempting the impossible. There is no such thing as truncating a double to 2 decimal places, because doubles don't have decimal places. They have binary places. See my answer here for proof. If you want decimal places you have to use a decimal radix, i.e. BigDecimal or DecimalFormat.
Issue is that floating point numbers are inherently approximate in nature, given the underlying representation. Therefore you will want to use them in places where approximations are good, and avoid them where approximations are no good (e.g. financials).
The call to rounded.doubleValue() still returns a floating point number and so it is still impacted by the limitations of the representation.
See
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
for more information.
The following piece of code helped me in restricting the number of decimal places (truncating) for a double value.
public static Double truncate (double valueToTruncate, int numberOfDecimalPlaces) {
if (valueToTruncate > 0) {
return new BigDecimal(String.valueOf(valueToTruncate)).setScale(numberOfDecimalPlaces, BigDecimal.ROUND_FLOOR).doubleValue();
} else {
return new BigDecimal(String.valueOf(valueToTruncate)).setScale(numberOfDecimalPlaces, BigDecimal.ROUND_CEILING).doubleValue();
}
}
Hope this helps someone :)
I'm new to this but keeping everything in front of me I did it this way. Now mind you this is a truncation mathematically, I don't convert to string except in my debugs after each line.
It isn't elegant but seems to work. This is purely a problem solving framework.
Anyway;
import java.util.Scanner;
public class TestConstructs
{
private static double w;
private static int w1;
private static double w2;
private static double w3;
private static int w4;
private static double w5;
private static double w6;
public static void main(String[] args)
{
// TODO Auto-generated method stub
TestConstructs foo = new TestConstructs();
foo.setWage(w);
}
public void setWage(double w)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter Wage: "); //enter something longish like 30987.978654 or w/e
w = input.nextDouble();
w1 = (int)w;
System.out.printf("%d w1\n",w1);
w2 = w - w1;
System.out.printf("%.3f w2\n",w2);
w3 = w2*100;
System.out.printf("%.3f w3\n",w3);
w4 = (int)w3;
System.out.printf("%d w4\n",w4);
w5 = (double)w4;
System.out.printf("%.3f w5\n",w5);
w6 = 0 + w5/100;
System.out.printf("%.3f w6\n",w6);
w = w1 + w6;
System.out.printf("%.3f final value\n",w); //.3 to show zero
input.close();
}
}
What I got at the end
Enter Wage:
30987.978654
30987 w1
0.979 w2
97.865 w3
97 w4
97.000 w5
0.970 w6
30987.970 final value

Decreasing accuracy of a double

Everything I find googling around says that either of the following will round a double down to two decimal places.
double roundToFourDecimals(double d)
{
DecimalFormat twoDForm = new DecimalFormat("#.##");
double myD = Double.valueOf(twoDForm.format(d));
return myD;
}
double nextLon = (double)Math.round(bnextLon * 100.0) / 100.0;
But neither works for me. The value of the double is 3.3743984E7 and the result is the same.
What's wrong?
Nothing's wrong. 3.3743984E7 is scientific notation. That is:
3.3743984E7 = 3.3743984 * 10^7 = 33743984
33,743,984.0 rounded to two decimal places is 33,743,984.0. If, perhaps you specified 33743984.05918, it would be rounded to 33743984.06, but both outputs would still say 3.3743984E7. (The preceding comment has been deleted due to invalidity found by #Sam.)
I can verify that your rounding code works:
public class Main {
public static void main(String[] args) {
double bnextLon = 275914.18410;
double nextLon = (double) Math.round(bnextLon * 100.0) / 100.0;
System.out.println(bnextLon + " became " + nextLon);
}
}
275914.1841 became 275914.18
I believe you simply need to determine what value you want in, and what value you want out. The code is giving you exactly what you're specifying.
3.3743984E7 means 33743984, so multiplying by 100 gives 3374398400, then rounding change nothing, then division goes back.
You should divide by 1E5 then round, than back.

round up to 2 decimal places in java? [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 7 years ago.
I have read a lot of stackoverflow questions but none seems to be working for me. i am using math.round() to round off.
this is the code:
class round{
public static void main(String args[]){
double a = 123.13698;
double roundOff = Math.round(a*100)/100;
System.out.println(roundOff);
}
}
the output i get is: 123 but i want it to be 123.14. i read that adding *100/100 will help but as you can see i didn't manage to get it to work.
it is absolutely essential for both input and output to be a double.
it would be great great help if you change the line 4 of the code above and post it.
Well this one works...
double roundOff = Math.round(a * 100.0) / 100.0;
Output is
123.14
Or as #Rufein said
double roundOff = (double) Math.round(a * 100) / 100;
this will do it for you as well.
double d = 2.34568;
DecimalFormat f = new DecimalFormat("##.00");
System.out.println(f.format(d));
String roundOffTo2DecPlaces(float val)
{
return String.format("%.2f", val);
}
BigDecimal a = new BigDecimal("123.13698");
BigDecimal roundOff = a.setScale(2, BigDecimal.ROUND_HALF_EVEN);
System.out.println(roundOff);
Go back to your code, and replace 100 by 100.00 and let me know if it works.
However, if you want to be formal, try this:
import java.text.DecimalFormat;
DecimalFormat df=new DecimalFormat("0.00");
String formate = df.format(value);
double finalValue = (Double)df.parse(formate) ;
double roundOff = Math.round(a*100)/100;
should be
double roundOff = Math.round(a*100)/100D;
Adding 'D' to 100 makes it Double literal, thus result produced will have precision
I know this is 2 year old question but as every body faces a problem to round off the values at some point of time.I would like to share a different way which can give us rounded values to any scale by using BigDecimal class .Here we can avoid extra steps which are required to get the final value if we use DecimalFormat("0.00") or using Math.round(a * 100) / 100 .
import java.math.BigDecimal;
public class RoundingNumbers {
public static void main(String args[]){
double number = 123.13698;
int decimalsToConsider = 2;
BigDecimal bigDecimal = new BigDecimal(number);
BigDecimal roundedWithScale = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println("Rounded value with setting scale = "+roundedWithScale);
bigDecimal = new BigDecimal(number);
BigDecimal roundedValueWithDivideLogic = bigDecimal.divide(BigDecimal.ONE,decimalsToConsider,BigDecimal.ROUND_HALF_UP);
System.out.println("Rounded value with Dividing by one = "+roundedValueWithDivideLogic);
}
}
This program would give us below output
Rounded value with setting scale = 123.14
Rounded value with Dividing by one = 123.14
Try :
class round{
public static void main(String args[]){
double a = 123.13698;
double roundOff = Math.round(a*100)/100;
String.format("%.3f", roundOff); //%.3f defines decimal precision you want
System.out.println(roundOff); }}
This is long one but a full proof solution, never fails
Just pass your number to this function as a double, it will return you rounding the decimal value up to the nearest value of 5;
if 4.25, Output 4.25
if 4.20, Output 4.20
if 4.24, Output 4.20
if 4.26, Output 4.30
if you want to round upto 2 decimal places,then use
DecimalFormat df = new DecimalFormat("#.##");
roundToMultipleOfFive(Double.valueOf(df.format(number)));
if up to 3 places, new DecimalFormat("#.###")
if up to n places, new DecimalFormat("#.nTimes #")
public double roundToMultipleOfFive(double x)
{
x=input.nextDouble();
String str=String.valueOf(x);
int pos=0;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='.')
{
pos=i;
break;
}
}
int after=Integer.parseInt(str.substring(pos+1,str.length()));
int Q=after/5;
int R =after%5;
if((Q%2)==0)
{
after=after-R;
}
else
{
if(5-R==5)
{
after=after;
}
else after=after+(5-R);
}
return Double.parseDouble(str.substring(0,pos+1).concat(String.valueOf(after))));
}
seems like you are hit by integer arithmetic: in some languages (int)/(int) will always be evaluated as integer arithmetic.
in order to force floating-point arithmetic, make sure that at least one of the operands is non-integer:
double roundOff = Math.round(a*100)/100.f;
I just modified your code. It works fine in my system. See if this helps
class round{
public static void main(String args[]){
double a = 123.13698;
double roundOff = Math.round(a*100)/100.00;
System.out.println(roundOff);
}
}
public static float roundFloat(float in) {
return ((int)((in*100f)+0.5f))/100f;
}
Should be ok for most cases. You can still changes types if you want to be compliant with doubles for instance.

How do I round a double to two decimal places in Java? [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 3 years ago.
This is what I did to round a double to 2 decimal places:
amount = roundTwoDecimals(amount);
public double roundTwoDecimals(double d) {
DecimalFormat twoDForm = new DecimalFormat("#.##");
return Double.valueOf(twoDForm.format(d));
}
This works great if the amount = 25.3569 or something like that, but if the amount = 25.00 or the amount = 25.0, then I get 25.0! What I want is both rounding as well as formatting to 2 decimal places.
Just use: (easy as pie)
double number = 651.5176515121351;
number = Math.round(number * 100);
number = number/100;
The output will be 651.52
Are you working with money? Creating a String and then converting it back is pretty loopy.
Use BigDecimal. This has been discussed quite extensively. You should have a Money class and the amount should be a BigDecimal.
Even if you're not working with money, consider BigDecimal.
Use a digit place holder (0), as with '#' trailing/leading zeros show as absent:
DecimalFormat twoDForm = new DecimalFormat("#.00");
Use this
String.format("%.2f", doubleValue) // change 2, according to your requirement.
You can't 'round a double to [any number of] decimal places', because doubles don't have decimal places. You can convert a double to a base-10 String with N decimal places, because base-10 does have decimal places, but when you convert it back you are back in double-land, with binary fractional places.
This is the simplest i could make it but it gets the job done a lot easier than most examples ive seen.
double total = 1.4563;
total = Math.round(total * 100);
System.out.println(total / 100);
The result is 1.46.
You can use org.apache.commons.math.util.MathUtils from apache common
double round = MathUtils.round(double1, 2, BigDecimal.ROUND_HALF_DOWN);
double amount = 25.00;
NumberFormat formatter = new DecimalFormat("#0.00");
System.out.println(formatter.format(amount));
You can use Apache Commons Math:
Precision.round(double x, int scale)
source: http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/util/Precision.html#round(double,%20int)
Your Money class could be represented as a subclass of Long or having a member representing the money value as a native long. Then when assigning values to your money instantiations, you will always be storing values that are actually REAL money values. You simply output your Money object (via your Money's overridden toString() method) with the appropriate formatting. e.g $1.25 in a Money object's internal representation is 125. You represent the money as cents, or pence or whatever the minimum denomination in the currency you are sealing with is ... then format it on output. No you can NEVER store an 'illegal' money value, like say $1.257.
Starting java 1.8 you can do more with lambda expressions & checks for null. Also, one below can handle Float or Double & variable number of decimal points (including 2 :-)).
public static Double round(Number src, int decimalPlaces) {
return Optional.ofNullable(src)
.map(Number::doubleValue)
.map(BigDecimal::new)
.map(dbl -> dbl.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP))
.map(BigDecimal::doubleValue)
.orElse(null);
}
You can try this one:
public static String getRoundedValue(Double value, String format) {
DecimalFormat df;
if(format == null)
df = new DecimalFormat("#.00");
else
df = new DecimalFormat(format);
return df.format(value);
}
or
public static double roundDoubleValue(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
long factor = (long) Math.pow(10, places);
value = value * factor;
long tmp = Math.round(value);
return (double) tmp / factor;
}
DecimalFormat df = new DecimalFormat("###.##");
double total = Double.valueOf(val);
First declare a object of DecimalFormat class. Note the argument inside the DecimalFormat is #.00 which means exactly 2 decimal places of rounding off.
private static DecimalFormat df2 = new DecimalFormat("#.00");
Now, apply the format to your double value:
double input = 32.123456;
System.out.println("double : " + df2.format(input)); // Output: 32.12
Note in case of double input = 32.1;
Then the output would be 32.10 and so on.
If you want the result to two decimal places you can do
// assuming you want to round to Infinity.
double tip = (long) (amount * percent + 0.5) / 100.0;
This result is not precise but Double.toString(double) will correct for this and print one to two decimal places. However as soon as you perform another calculation, you can get a result which will not be implicitly rounded. ;)
Math.round is one answer,
public class Util {
public static Double formatDouble(Double valueToFormat) {
long rounded = Math.round(valueToFormat*100);
return rounded/100.0;
}
}
Test in Spock,Groovy
void "test double format"(){
given:
Double performance = 0.6666666666666666
when:
Double formattedPerformance = Util.formatDouble(performance)
println "######################## formatted ######################### => ${formattedPerformance}"
then:
0.67 == formattedPerformance
}
Presuming the amount could be positive as well as negative, rounding to two decimal places may use the following piece of code snippet.
amount = roundTwoDecimals(amount);
public double roundTwoDecimals(double d) {
if (d < 0)
d -= 0.005;
else if (d > 0)
d += 0.005;
return (double)((long)(d * 100.0))/100);
}
where num is the double number
Integer 2 denotes the number of decimal places that we want to print.
Here we are taking 2 decimal palces
System.out.printf("%.2f",num);
Here is an easy way that guarantee to output the myFixedNumber rounded to two decimal places:
import java.text.DecimalFormat;
public class TwoDecimalPlaces {
static double myFixedNumber = 98765.4321;
public static void main(String[] args) {
System.out.println(new DecimalFormat("0.00").format(myFixedNumber));
}
}
The result is: 98765.43
int i = 180;
int j = 1;
double div= ((double)(j*100)/i);
DecimalFormat df = new DecimalFormat("#.00"); // simple way to format till any deciaml points
System.out.println(div);
System.out.println(df.format(div));
You can use this function.
import org.apache.commons.lang.StringUtils;
public static double roundToDecimals(double number, int c)
{
String rightPad = StringUtils.rightPad("1", c+1, "0");
int decimalPoint = Integer.parseInt(rightPad);
number = Math.round(number * decimalPoint);
return number/decimalPoint;
}

Categories

Resources