package simpleenglishlanguagecalculator;
import java.util.Scanner;
/**
*
* @author joshu
*/
public class SimpleEnglishLanguageCalculator {
public static void main(String[] args) {
// TODO code application logic here
// Now we will make a Scanner object to read user input.
Scanner input = new Scanner(System.in);
// Now we will ask the user to enter the first number.
System.out.print("Enter the first number: ");
double firstNum = input.nextDouble();
// Now we will ask the user to enter the second number.
System.out.print("Enter the second number: ");
double secondNum = input.nextDouble();
// Now we will ask the user to enter the third number.
System.out.print("Enter the third number: ");
double thirdNum = input.nextDouble();
// Now we will ask the user to enter any operator.
System.out.print("Enter any one operator (+, -, *, /, ^): ");
char operator = input.next().charAt(0);
// Now we will end scanner object.
input.close();```
#Hi, i am trying to get my code for the '^' operator to print the result in the 2e format.
32 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @ivory girder! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
// Now we will use a switch statement to determine the action based on the user chosen operator.
switch (operator) {
case '+':
System.out.println(firstNum + " plus " + secondNum + " plus " + thirdNum + " is " + (firstNum + secondNum + thirdNum));
break;
case '-':
System.out.println(firstNum + " minus " + secondNum + " minus " + thirdNum + " is " + (firstNum - secondNum - thirdNum));
break;
case '*':
System.out.println(firstNum + " multiplied by " + secondNum + " multiplied by " + thirdNum + " is " + (firstNum * secondNum * thirdNum));
break;
case '/':
if (secondNum == 0 || thirdNum == 0) {
System.out.println("Division by zero is not allowed.");
} else {
System.out.println(firstNum + " divided by " + secondNum + " divided by " + thirdNum + " is " + (double)(firstNum / secondNum / thirdNum));
}
break;
case '^':
double result = Math.pow(firstNum, Math.pow(secondNum, thirdNum));
System.out.println(firstNum + " to the power of " + secondNum + " to the power of " + thirdNum + " is " + result + " (approx. " + String.format("%.2e", result) + ")");
break;
default:
System.out.println(operator + "the operator is not a valid one");
break;
}
}
}```
for case '^"
the output I am getting on netbeansIDE16
is
Enter the second number: 7
Enter the third number: 6
Enter any one operator (+, -, *, /, ^): ^
8.0 to the power of 7.0 to the power of 6.0 is Infinity (approx. Infinity)```
and I want to get 8 to the power of 7 to the power of 6 is
8.5070592e+37.
you would need to use BigInteger/BigDecimal for that, double doesn't have that precision and i'm not sure long goes that hi (but it would of course not be sufficient for higher cases)
yeah i'm trying that out now
i'm getting a lot of errors
yeah you can't use operators directly on BigDecimal
it has its own methods
(this isn't about your actual question, btw. but you're getting Infinity from floating-point arithmatic doing rounding.)
ohh how do I stop it for doing rounding
you can't, that's why you have to use BigDecimal in its place
ohh I see
float and double are both fixed size, 32bit and 64bit, that gives them limited precision, and that precision applies for both large numbers and precise decimals
/run
System.out.println(0.1 + 0.2);
Here is your java(15.0.2) output @trim condor
0.30000000000000004
try checking out https://0.30000000000000004.com
anyways, BigDecimal is special, it's not fixed size, and it implements arbitrary precision
well, in decimal at least
it's still not exact precision, since you still can't represent 1/3 in decimal
anyways, 
https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/math/BigDecimal.html can tell you how to do your operations
also fyi, "'^' operator" doesn't exactly work, java doesn't have an operator for this, most other languages that do use **, and ^ is used as bitwise XOR, so using this it's not very clear you're talking about pow/exponentials
oh yeahh i was just using '^' cause it was the operator that the user was choosing
yeah that would be the math context so it does make sense there, but that wasn't clear in the title of the post, that's all
yeah my fault