How to compare case classes with floating numbers in Scalatest? - scala

Can you compare two objects that have floating numbers in field or values of maps, without manually comparing each field; i.e., enforce tolerance whenever floats or doubles are encountered using custom matchers and Scala generics?

Related

spark: overflow / underflow for Decimal / BigDecinal

just trying to find if there is a way to detect overflow/underflow for DecimalType column in spark (DataFrame API)? there is a method to detect this happening for numeric types with a small range of values.
I understand that could be done on the lower level via using UDFs and detecting if there is an overflow in the corresponding BigDecimal value representing result, but I would prefer to use the existing DataFrame function etc.

How to tell if two numbers are really different or they are actually the same due to floating point error

For example,
0.168033639538270
and
0.168033639538270
are two double type numbers that are from two different calculations (some further calculations from the eigenvalues of a matrix).
But they are treated as different by MATLAB (by unique or ==). How do I know if MATLAB treats them as different due to floating point error eps = 2.220446049250313e-16, or if they are actually different (the digits behind the first 15 digits are not the same, but MATLAB just will not display them). Sometimes MATLAB treats two number with the same display value as the same, but sometimes different, so I want to know if they are really different.
You can print a formatted version of the number at required precision using sprintf, and then compare the two strings using strcmp.

Representation of fixed point number in systemverilog

How should I represent a fixed point number in systemverilog since it doesnt support fixed point numbers for reg and logic.Is using real data type the correct method or can we use any different data type?
I am trying to do a square root function in systemverilog, in which the result will be in FP e.g sqrt(8) = 2.82.
What should be the data type of my inputs and outputs(sqrt) such that I can check the decimal point places correctly while verifying.
You use integral types for fixed point numbers. Some people will index their variables like
logic [M-1:-F] fp_number; // M-bits integer, F bit fractional
But it is up to you to adjust the decimal point when adding different sized numbers as well as adjusting for multiplication and division. There are some OpenCore libraries that have many of these operations for you.

What's the correct number type for financial variables in Swift?

I am used to programming in Java, where the BigDecimal type is the best for storing financial values, since there are manners to specify rounding rules over the calculations.
In the latest swift version (2.1 at the time this post is written), which native type better supports correct calculations and rounding for financial values? Is there any equivalent to java's BigDecimal? Or anything similar?
You can use NSDecimal or NSDecimalNumber for arbitrary precision numbers.
See more on NSDecimalNumbers's reference page.
If you are concerned about storing for example $1.23 in a float or double, and the potential inaccuracies you will get from floating point precision errors, that is if you actually want to stick to integer amounts of cents or pence (or whatever else). Then use an integer to store your value and use the pence/cent as your unit instead of pounds/dollars. You will then be 100% accurate when dealing in integer amounts of pence/cents, and it's easier than using a class like NSDecimalNumber. The display of that value is then purely a presentation issue.
If however you need to deal with fractions of a pence/cent, then NSDecimalNumber is probably what you want.
I recommend looking into how classes like this actually work, and how floating point numbers work too, because having an understanding of this will help you to see why precision errors arise and just what the precision limits are of a class like NSDecimalNumber, why it's better for storing decimal numbers, why floats are good at storing numbers like 17/262144 (i.e. where the denominator is a power of two) but can't store 1/100, etc.

Internal representation of Enumeration Types in NuSMV/NuXMV

why is there a significant performance dropdown when representing a 16-bit signed integer variable as an intervall (-32768..32767) in comparison to fixed length bit arrays?
Inspecting the pre-processed NuSMV/NuXMV model one can observe that the interval types are converted to enumerations.
The statistics of the BDD however does not show any relevant informaton.