0

Using java.math.BigDecimal in VS throws a plethora of errors and exceptions. here is the code:

using java.math;
using System.Globalization;
using System.Numerics;


namespace single_num_fib_solver
{
    internal class Program
    {
        
        static void Main(string[] args)
        {
            // getting the number they want to solve for.
            Console.WriteLine("Please input the Fibanachi number you wat to solve for, then press enter.");
            int.TryParse(Console.ReadLine(), out var solvenumber);
            solvenumber--;
            // using Binet’s Formula to solve for the number.
            BigDecimal sqrt = new BigDecimal(Math.Sqrt(5));
            BigDecimal TWO = new BigDecimal(2);
            BigDecimal num1 = sqrt.add(BigDecimal.ONE).divide(TWO);
            BigDecimal num2 = BigDecimal.ONE.subtract(sqrt).divide(TWO);
            BigDecimal num3 = num1.pow(solvenumber);
            BigDecimal num4 = num2.pow (solvenumber);
            BigDecimal num5 = num3.subtract(num4).divide(sqrt); 
            // outputting the nuber that was solved for. 
            Console.WriteLine(num5);

        }        
    }
}

I have tried using BigIntegers and the code didn't throw all the exceptions, but the rounding of the numbers didn't allow for the acuity required for the formula.

here are the errors it outputs:

Unhandled exception. System.TypeInitializationException: The type initializer for 'java.math.BigDecimal' threw an exception.
 ---> System.TypeInitializationException: The type initializer for '1' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'java.lang.ThreadLocal' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'java.util.concurrent.atomic.AtomicInteger' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'sun.misc.VM' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'java.nio.charset.Charset' threw an exception.
 ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at java.lang.ThreadLocal.nextHashCode()
   at java.lang.ThreadLocal..ctor()
   at ikvm.internal.IntrinsicThreadLocal..ctor()
   at java.nio.charset.Charset.__<tls>_0..ctor()
   at java.nio.charset.Charset..cctor()
   --- End of inner exception stack trace ---
   at java.nio.charset.Charset.forName(String charsetName)
   at java.io.PrintStream.toCharset(String csn)
   at java.io.PrintStream..ctor(OutputStream out, Boolean autoFlush, String encoding)
   at java.lang.System.newPrintStream(FileOutputStream fos, String enc)
   at java.lang.System.initializeSystemClass()
   at __<MethodAccessor>__System__initializeSystemClass()
   at IKVM.Runtime.Accessors.Java.Lang.SystemAccessor.InvokeInitializeSystemClass()
   at IKVM.Runtime.JVM.EnsureInitialized()
   at IKVM.Java.Externs.sun.misc.VM.initialize()
   at sun.misc.VM.initialize()
   at sun.misc.VM..cctor()
   --- End of inner exception stack trace ---
   at sun.misc.VM.isSystemDomainLoader(ClassLoader loader)
   at sun.misc.Unsafe.getUnsafe(CallerID )
   at java.util.concurrent.atomic.AtomicInteger..cctor()
   --- End of inner exception stack trace ---
   at java.util.concurrent.atomic.AtomicInteger..ctor()
   at java.lang.ThreadLocal..cctor()
   --- End of inner exception stack trace ---
   at java.lang.ThreadLocal.__<clinit>()
   at java.math.BigDecimal.1..cctor()
   --- End of inner exception stack trace ---
   at java.math.BigDecimal.1..ctor()
   at java.math.BigDecimal..cctor()
   --- End of inner exception stack trace ---
   at java.math.BigDecimal..ctor(Double val)
   at single_num_fib_solver.Program.Main(String[] args) in C:\Users |hidden path| Program.cs:line 18

Here is the minimum code required to recreate the issue:

using java.math;
using System.Globalization;
using System.Numerics;


namespace single_num_fib_solver
{
    internal class Program
    {

        static void Main(string[] args)
        {
            BigDecimal test = new BigDecimal(5);
        }
    }
}
  • Please provide a [minimal reproducible example](http://stackoverflow.com/help/minimal-reproducible-example) including building instructions. – aled May 31 '23 at 20:22
  • And how do you build and execute it? I see IKVM mentioned in the stacktrace. How are you using it? Is the error happening at execution time or compile time? – aled May 31 '23 at 20:29
  • @aled the program will execute normally up until it is required to do any math with the ```BigDecimal``` I am relatively new at C# so forgive me for any ridiculous errors. – Circuit_Master May 31 '23 at 20:32

1 Answers1

-1

Mixing languages is a bad idea if you are not proficient with both. You seem to be using IKVM to be able to use Java classes. Such an approach may work but also you may hit limitations. It is better to use a native library for your language. See this answer for suggestions of alternatives: What is the equivalent of the Java BigDecimal class in C#?

aled
  • 21,330
  • 3
  • 27
  • 34