What is NaN in Flutter and what is caused by? - flutter

When I run an app where an icon, wrapped in a Positioned is drawn on a container, I get an error stating that the offset, (Positioned's left) uses a NaN value. I took a look online and I found that NaN means Not a Number, but I couldn't find what causes it. I mean, If I divide a number by Zero I'll get an error saying that I cannot divide a number by zero, but NaN is a constant for what I understood, and if I was multiplying a number let's say for a string I'd get incompatible types error, instead of NaN. I'm not asking for an answer specific for my code as I couldn't provide it, but rather a more general explanation.

NaN could be caused by dividing by a variable which cannot be cast to a numbe as #JoSSte stated.
But as I found out empirically, Flutter does not throw an error while dividing by 0, as opposed to python for example where ZeroDivisionError is thrown. Flutter sees the output of a division by zero as infinity or -infinity, when dividing respectively a positive and a negative number by zero.
So this makes sense, but in instances when infinity cannot be accepted as a value (as it isn't) it throws an error stating there was an error in the framework itself.

Related

How to determine an Overflow in a 4 bit ripple-carry adder-substractor?

What function that depends by following variablesc (First Operand's sign[0/1], Second Operand's sign[0/1], Result's sign[0/1] and Operation sign[0/1]) can identify an overflow in the 4-bit ripple-carry adder/substractor?
P.S.
An overflow occurs only if:
the sum of two positive numbers yields a negative result, the sum has overflowed.
the sum of two negative numbers yields a positive result, the sum has overflowed.
I only know the method with checking the 2 last carries but it seems that there's another method.
Your PS already contains the correct logic formula written in prose (for addition). Remember that a number is "positive" if its sign bit is zero and the number is negative if its sign bit is one.1 This means you can translate "yields a negative result" to "the operation sign is 1". You can translate the other statements about operands or results to logic conditions in the same way, to finally derive a general boolean formula.
1: I know that zero is neither positive nor negative, but treating zero as positive does no harm in this case.

Why does assertion fail when values are equal

I'm writing my first test in unity3d. My objective is to assert that as the player runs, the Z axis remains consistent and the height doesn't change. In other terms, the Character's z position is consistently a 1 value.
This test continuously fails with the output of:
AssertionException: FloatComparer assertion failed.
Character.Transform.up.z CompareToConstantValue 1 failed. Expected: 1 Actual: 1
Created in
UnityEngine.Debug:LogException(Exception, Object)
UnityTest.ActionBase:Fail(AssertionComponent) (at Assets/UnityTestTools/Assertions/Comparers/ActionBase.cs:93)
UnityTest.Assertions:CheckAssertions(AssertionComponent[]) (at Assets/UnityTestTools/Assertions/Assertions.cs:37)
UnityTest.Assertions:CheckAssertions(AssertionComponent) (at Assets/UnityTestTools/Assertions/Assertions.cs:18)
UnityTest.AssertionComponent:CheckAssertionFor(CheckMethod) (at Assets/UnityTestTools/Assertions/AssertionComponent.cs:251)
UnityTest.AssertionComponent:Update() (at Assets/UnityTestTools/Assertions/AssertionComponent.cs:148)
The expected/actual values are the same so I'm struggling to see what the problem is. Any insight would be appreciated.
Because it's a float, I tried using 1.0 as the constant value with no luck.
The value might not be exactly 1, but your assertion tells the test to expect it as EXACTLY 1.
But floating points variable are not to safe to test to exact values.
So you could try allow a minimum difference of your wanted value using the "Floating Point Error" field in the inspector.
For example if your value is expected to be 1
But in reality your floating point (can be) 0.99998...
Then you can set Floating Point error to 0.0001
So the assertion will pass with values between 0.999900000... and 1.000100000...
And your value of 0.99998 woule be in between and pass.
This issues ocure with all floating point numbers so also with the values of vector pistions for example
EDIT:
So the exception you see is also having this issue thinking that actual is 1 but it isn't, it's... nearly almoast 1^^

Subtracting matrices of unequal dimension

When I try to do the following command I get an error.
err = sqrt(mean((xi256-xc256).^2))
I am aware that the matrix sizes are different.
whos xi256 xc256` gives:
Name Size Bytes Class Attributes
xc256 27x1 216 double
xi256 513x1 4104 double
I am supposed to negate find the difference of these two matrices. In fact the command given at the top was in the course notes and the course has been running for several years! I have tried googling ways to resolve this error to perform this subtraction regardless but have found no solution. Maybe one of the matrices can be scaled to match the dimensions of the other? However, I have not been able to find any such functions that would let me do this.
I need to find the RMS error in a given set of data. xc256 was calculated through a numerical method, xi256 gives the true value.
Edit: I was able to use another set of results.
First check that xc256 is correctly computed and that you do not have a matrix transposition gone wrong or something like that. Computing the RMS between two vector of different sizes does not make sense, and padding or replicating will get you rid of the error, but is most probably not what you actually want.
There are just two situations that I can think of, I will list them here:
The line is wrong (not likely as it looks pretty normal, but make sure to check the book!)
The input of the line is wrong
Assuming it is in point 2, again there are two possibilities:
xi256 is of incorrect size (likelyhood of this depends on how you got it)
xc256 is of incorrect size
Assuming it is again point 2, there are yet again 2 likely possibilities:
xc256 should be a scalar
xc256 should be a vector with the same size as xi256
From here on there is insufficient information to continue, but check whether you accidentally made it 27 times as long, or 19 times too short somewhere. Just use some breakpoints throughout the code to see how the size develops.

Issue in viewing double values

I am having an issue with viewing double data in matlab console. Actually, I am importing a matrix from my data file. The value of a particular row and column was 1.543 but in the console when I use disp(x) where x is the matrix imported, it is showing as 1.0e+03 * 0.0002. However, when I try to access that particular element in the matrix using disp(x(25,25)) where 25 and 25 are the row and column numbers it is showing to be 1.543. So I am confused. Any clarifications. It is just that when I print the whole matrix it is showing as 1.0e+03 * 0.0002.
The following command should fix it. It is only a display issue, the precision of the actual values in the matrix are not affected:
format shortG
That happens due to high dynamic range of your data.
Try for example :
x = [10^-10 10^10];
disp(x);
The result is:
1.0e+010 *
0.0000 1.0000
It looks like the first value is zero, but it isn't. It is almost zero compared to the second one. That is not surprising. Try to add to the big value the small one, and subtract, and you get zero. That is due to floating point arithmetic.The following expression is true
isequal( (x(1)+x(2)) - x(2) , 0)
What can be done?
1) A really high dynamic range can cause troubles in any kind of computations. Try to understand where it came from, and solve the problem in a broader context.
2). You can try to set
format long
It can improve the situation visually for some of the cases.

Why do we have "is not a Number" (isNan) functions?

Many languages have an isNaN() function. I am asking myself: why check for not being a number?
Is the reason purely logical or is it faster to check for not a number instead of is a number?
Note that this is a pure question of understanding. I know that I can negate isNaN() to achieve an isNumber() function for example.
However I am searching for a reason WHY we are checking for not a number?
In computing, NaN (Not a Number) is a
value of numeric data type
representing an undefined or
unrepresentable value, especially in
floating-point calculations.
Wiki Article
Because Not a Number is a special case of an expression.
You can't just use 0 or -1 or something like that because those numbers already have meanings.
Not a Number means something went awry in a calculation and a valid number cannot be computed out of it.
It's on the same line of thinking as having null. Sure, we could assign an arbitrary numerical value to mean null but it would be confusing and we'd hit all sorts of weird errors on corner cases.
'Not a Number' is the result of specific floating point calculations. It's not about "hey, is this variable holding 120 or "abc"?'
isThisCaseExceptional seems more reasonable to me than isEverythingNormal because I'm likely to write
possible_number = some_calculation();
if (inNaN(possible_number)) handle_the_surprise;
// .. keep going
instead of
possible_number = some_calculation();
if (inANumber(possible_number)) {
// .. keep going
} else {
// handle the surprise
}
The check is for whether it is not a number, because the assumption is that it is a number. NaN is the exceptional case when you're expecting a numeric value, so it makes sense to me that it is done this way. I'd rather check for isNaN infrequently than check if a value isNum frequently, after all.
It is a way to report an error condition (mathematically undefined or outside of technical limits).
Many languages have special representations if the result of a computation is not representable by a number, for example NaN and Inf. So isNaN() checks, if a result is actually a number or the special marker for NaN.
NaNs are used for:
Nonreal or undefined values.
Error handling. Initializing a variable with NaN allows you to test for NaN and make sure it has been set with a valid value.
Objects that ignore a member or property. For example, WPF uses NaN to represent the Width and Height of visual elements that do not define their own size.