-3

If the number of digits is infinite then I mark it as irrational and everything else is rational as it would be finite.

I tired input 3.14 but it crashed and didn't compile the output of irrational or rational.

import java.math.BigDecimal;

import java.util.Scanner;

public class non_terminating_decimals {
    public static void main(String[] args) {

        Scanner inputNumber = new Scanner(System.in);
        System.out.println("input number : ");
        BigDecimal inputnumber = inputNumber.nextBigDecimal();
        BigDecimal numerofDigits = input(new BigDecimal(String.valueOf(inputnumber)));

        BigDecimal infinity = BigDecimal.valueOf(Double.POSITIVE_INFINITY);

        if (numerofDigits == infinity) {

            System.out.println("Irrational");
        }
        else {

            System.out.println("Rational");
        }
    }

    static int integerDigits(BigDecimal number) {
        return number.signum() == 0 ? 1 : number.precision() - number.scale();
    }

    static BigDecimal input(BigDecimal number) {
        return BigDecimal.valueOf(0);
    }
}
  • What's the error you get when you try to compile it? (eg. when you run `javac`) – Roddy of the Frozen Peas Aug 09 '22 at 22:25
  • Exception in thread "main" java.lang.NumberFormatException: Character I is neither a decimal digit number, decimal point, nor "e" notation exponential mark. – ColorTheory42 Aug 09 '22 at 22:40
  • You're writing a program to check whether the user has typed an infinite number of digits? How long do you expect it will take the user to type those digits? – Dawood ibn Kareem Aug 09 '22 at 22:42
  • I'm going to add stuff like pi inputs and detect if its a string then change that input to the pi command. and also add a "..." detector to simulate recurring digits. – ColorTheory42 Aug 09 '22 at 22:44
  • I assume it will calculate every digit. all the way to infinity and beyond. – ColorTheory42 Aug 09 '22 at 22:45
  • 2
    You're confusing 'compile' and 'run'. If you got a NumberFormatExtension, then that's a runtime thing, not a compile-time problem. – access violation Aug 09 '22 at 22:48
  • @ColorTheory42 `BigDecimal infinity = BigDecimal.valueOf(Double.POSITIVE_INFINITY);` Causes the error since BigDecimal has no concept of infinity. All known irrationals you can just implement with special handling, things like PI, e, etc. But on computer you cant even calculate an irrational number because of physical limitations. All numerical values on a computer are finite and by definition are rational. – Marko Taht Aug 09 '22 at 22:51
  • @ColorTheory42 Best you can do is to approximate the answer. Set some realistic finite value as the rational/irrational break point, And whenever a number has more decimal places than this value, it is regarded as irrational. Reason being, that sooner or later your computer will run out of memory, and whatever number is in memory by that time is finite and by definition it is rational. Any special numbers or symbols you add need special handling. – Marko Taht Aug 09 '22 at 22:58
  • I really don't think you've thought this through. A user can't type an infinite number of digits. Computations that take place inside a computer generally work with data types that can only store rational numbers. I can't imagine any scenario where your program is going to be able to look at its input, and conclude that it's irrational. – Dawood ibn Kareem Aug 09 '22 at 23:06
  • Also, you shouldn't use the word "compile" in a question if that's not actually what you mean. – Dawood ibn Kareem Aug 09 '22 at 23:08
  • The user can't even type a finite number of digits N, where N is the maximum possible value of Java double-precision floating point number; that's about 10-to-the-power-308 decimal digits, right? I suspect some confusion between "a very big number" and "the number of digits in a very big number". – access violation Aug 09 '22 at 23:08

1 Answers1

4

Let's unpack this statement:

BigDecimal infinity = 
    BigDecimal.valueOf(Double.POSITIVE_INFINITY);

Double.POSITIVE_INFINITY is some number.

Looking at the documentation for BigDecimal.valueOf, we see it uses Double.toString() to do the conversion.

Looking at the documentation for that, we see that a value of positive infinity results in the string "Infinity".

Thus, we're effectively left with trying to evaluate

BigDecimal("Infinity");

And if we look at the documentation for that particular constructor, there's no suggestion it can handle non-numeric string arguments.

  • This issue can be solved by creating a custom implementation of BigDecimal where it can handle some special values like: Infinity, PI, euler number, other known and named irrational numbers. But given that a computer cannot handle any irrational that comes from some calculation, their entire program idea becomes either impossible, or they need to work wihtin a very restricted space and do some major assumptions that ultimately leave the result be approximation, rather than a true value. – Marko Taht Aug 10 '22 at 06:51
  • When the BigDecimal("Infinity") problem is fixed, I think the program will give correct results - it will print "rational" for all possible input numbers :-) – access violation Aug 10 '22 at 12:36