acmerobotics / ftc-dashboard

React-based web dashboard designed for FTC
https://acmerobotics.github.io/ftc-dashboard
Other
178 stars 140 forks source link

Throws "Infinity is not a valid double value" when doing integer division on a Dashboard controlled double field #38

Closed NoahBres closed 3 years ago

NoahBres commented 3 years ago

The app will throw the following error when doing integer division on a double field:

fi.iki.elonen.NanoHTTPD: java.lang.IllegalArgumentException: Infinity is not a valid double value as per JSON specification. To override this behavior, use GsonBuilder.serializeSpecialFloatingPointValues() method.

I was able to reliably reproduce that error with the following two cases:

@Config
class Test {
    public static double case1 = Double.POSITIVE_INFINITY;
    public static double case2 = 1 / 3;

    // These are fine
    public static double correct1 = 1.0 / 3.0;
    public static double correct2 = 3.0 / 1.0;
} 

Obviously, case 1 is expected to throw that error. As would any division by zero. But I'm not sure why any integer division throws an error. I would think that in case 2, it evaluates 1/3 using integer division, resulting in zero, and then casting that to a double. Clearly, I don't know enough about Java's type coercion or reflection system but I would assume the second case would just result in a double zero value?

NoahBres commented 3 years ago

Never mind. I was wondering if this was an error in NanoHTTPD itself so googled the error and the first result was a previously closed issue but on the quickstart repo instead :P

https://github.com/acmerobotics/road-runner-quickstart/issues/22

For anyone stumbling upon this in the future, the issue isn't caused by a standalone field. Case 2 actually works fine. It just casts to a zero. It's caused by kV dividing by GEAR_RATIO (which was casted to zero) resulting in an infinity value. Primary issue was ambiguity in the source of the error and neglecting to isolate the problem in its own repo.