My program consists of a Java and a Python component and I want Python to format floating point numbers exactly in the same way as Java does by default.
Java:
float number1 = (float) Math.PI;
float number2 = (float) Math.PI / 1000f;
float number3 = (float) Math.PI / 10000f;
float number4 = (float) Math.PI / 100000f;
float number5 = (float) (int)(Math.PI * 1000000f) / 1000000f / 100000f;
System.out.println(number1); // prints 3.1415927
System.out.println(number2); // prints 0.0031415927
System.out.println(number3); // prints 3.1415926E-4
System.out.println(number4); // prints 3.1415926E-5
System.out.println(number5); // prints 3.141592E-5
How to get the same output in Python?
First attempts in Python:
import math
number1 = float(math.pi)
number2 = float(math.pi / 1000)
number3 = float(math.pi / 10000)
number4 = float(math.pi / 100000)
number5 = float(int(math.pi * 1000000) / 1000000 / 100000)
print(number1) # prints 3.141592653589793
print(number2) # prints 0.0031415926535897933
print(number3) # prints 0.0003141592653589793
print(number4) # prints 3.141592653589793e-05
print(number5) # prints 3.141592e-05
print("{:8e}".format(number1)) # prints 3.141593e+00
print("{:8e}".format(number2)) # prints 3.141593e-03
print("{:8e}".format(number3)) # prints 3.141593e-04
print("{:8e}".format(number4)) # prints 3.141593e-05
print("{:8e}".format(number5)) # prints 3.141592e-05
Your formatting syntax is incorrect. Do this instead
print("{:.15f}".format(math.pi))
or the old fashioned way
print("%.15f" % (math.pi))
Related
Here are the expressions I'm working with:
float firstValue = (float) (5 / 2); //output is 2.0
float secondValue = (float) 5 / 2; //output is 2.5
I'm stumped here and can't figure out why this type casting is returning two different values. I understand I can just do (5f / 2f) but I wanted to experiment using the other type casting with an expression. Why is firstValue 2.0 and secondValue 2.5? Where did the .5 go?
As brackets have the highest precedence, they get solved first
float firstValue = (float) (5 / 2); // division of integers
= (float) (2); // 5/2 = 2 , as Integers are being divided
= 2f
float secondValue = (float) 5 / 2; // division of float with integer
= ((float) 5) / 2;
= 5f / 2; // second value is equivalent to this
= 2.5f // as Float divided by Integer is Float
The first is integer math. This
float firstValue = (float) (5 / 2);
First divides five by two and gets two. Then it converts two to 2.0. The second is floating point math.
float secondValue = 5f / 2;
Which is 2.5 (and a float). Because a float divided by an int is a float.
float firstValue = (float) (5 / 2); // division of integers
The first step is to do 5/2 calculation.Then the answer is given in float.If you explain further 5 and 2 are int numbers. After calculating the int for two int numbers, the final answer is returned by int. Here the final int answer (2) is converted to a float answer. That is, wider conversion is used here. So the final answer is the integer value(2) shown in float form(2.0).
2.float secondValue = (float) 5 / 2; //output is 2.5
Since the first value(5) is named a float number, the final answer is the decimal itself
I have the following code:
String price="9000.89";
double basePrice =10000;
String dis= String.valueOf(((basePrice - Double
.parseDouble(price)) * 100 / basePrice));
System.out.println(dis);
It is giving me the output as "9.991100000000007", but i need it in 2 decimal points. Any idea how to do that?
Expected output : 9.99
String price="9000.89";
double basePrice =10000;
String dis = String.format("%.2f",((basePrice - Double.parseDouble(price)) * 100 / basePrice));
System.out.println(dis);
I would recommend
double dis = ((basePrice - Double.parseDouble(price)) * 100 / basePrice);
System.out.format("%1.2f", dis);
You can round using:
Math.round(number * 100.0) / 100.0;
This is what I did but no matter what I keep getting infinity:
public double calcr(){
double cot = 1 / Math.tan(0);
return .5 * sideLength * cot * (Math.PI / numSides);
}
Main:
RegularPolygon poly = new RegularPolygon(4, 10);
System.out.println(poly.calcr());
Output:
Inifinity
What am I doing wrong?
The problem is that you do
double cot = 1 / Math.tan(0);
which will make cot be Infinity.
You'd want:
double cot = 1 / Math.tan(Math.PI / numSides);
return .5 * sideLength * cot;
Or, in a single line:
return .5 * sideLength / Math.tan(Math.PI / numSides);
tan(0) is 0, so this line
double cot = 1 / Math.tan(0);
sets cot to Infinity. The computation below it will then also evaluate to Infinity, as you saw.
Since it looks like you're trying to evaluate cot(pi/n), you'd need 1 / Math.tan(Math.PI / n) instead of using cot * (Math.PI / numSides) with an incorrect value for cot.
I have a Java class and im stumped on this issue. We have to make a volume calculator. You input the diamater of a sphere, and the program spits out the volume. It works fine with whole numbers but whenever I throw a decimal at it, it crashes. Im assuming it has to do with the precision of the variable
double sphereDiam;
double sphereRadius;
double sphereVolume;
System.out.println("Enter the diamater of a sphere:");
sphereDiam = keyboard.nextInt();
sphereRadius = (sphereDiam / 2.0);
sphereVolume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( sphereRadius, 3 );
System.out.println("The volume is: " + sphereVolume);
So, like I said if i put in a whole number, it works fine. But I put in 25.4 and it crashes on me.
This is because keyboard.nextInt() is expecting an int, not a float or double. You can change it to:
float sphereDiam;
double sphereRadius;
double sphereVolume;
System.out.println("Enter the diamater of a sphere:");
sphereDiam = keyboard.nextFloat();
sphereRadius = (sphereDiam / 2.0);
sphereVolume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( sphereRadius, 3 );
System.out.println("The volume is: " + sphereVolume);
nextFloat() and nextDouble() will pickup int types as well and automatically convert them to the desired type.
double sphereDiam;
double sphereRadius;
double sphereVolume;
System.out.println("Enter the diameter of a sphere:");
sphereDiam = keyboard.nextDouble();
sphereRadius = (sphereDiam / 2.0);
sphereVolume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( sphereRadius, 3 );
System.out.println("");
System.out.println("The volume is: " + sphereVolume);
what I'm doing is this, I leave the 5' alone then I turn 3" into a decimal of a foot by dividing by 12 then I divide the numerator by the denominator then I add it all up and multiply by 1.414 it works but I dont know how I would display the foot inches and fraction of a inch
c2c_fdecimal = f_num / f_den;
c2c_fdeci_fft = c2c_fdecimal / 12.0;
deci_of_foot = inchs / 12.0;
total_travel= feet + c2c_fdeci_fft + deci_of_foot;
toff_ftodeci = tkoff_numa / tkoff_dena;
tkoff_inch = tkoff_inch / 12.0;
sub_toff = toff_ftodeci / 12.0 + tkoff_inch;
ans = (total_travel * ffformula) - sub_toff;
//print out measurement format
ansint = (int)ans;
strip_inches = (int) ((ans - ansint) * 12.0);
//print answer
editText2.setText(ansint + " ft" + strip_inches + " in");
Here's how you'd figure out the feet and inches in Java:
double resultInInches; // you start with inches...
int feet = (int)(resultInInches / 12);
double rest = (resultInInches / 12) - feet;
int wholeInches = (int)rest;
rest = rest-wholeInches; // rest now holds the fraction of the inch, eg 0.4141
Now all that's left to do is display rest as a fraction. I'm not familiar with what is or is not permitted in the Android SDK, and there's a bunch of ways to do this (see this answer).