#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)

ivory girder
#
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();```
meager radishBOT
#

This post has been reserved for your question.

Hey @ivory girder! Please use /close or the Close Post button 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.

ivory girder
#
// 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.

trim condor
#

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)

ivory girder
#

yeah i'm trying that out now

trim condor
#

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.)

ivory girder
trim condor
#

you can't, that's why you have to use BigDecimal in its place

ivory girder
#

ohh I see

trim condor
#

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);
severe questBOT
#

Here is your java(15.0.2) output @trim condor

0.30000000000000004
trim condor
#

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, readdocs

#

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

ivory girder
trim condor
#

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

ivory girder
#

yeah my fault