Why I am getting negative result for negativeValue.abs()? [duplicate] - flutter

This question already has an answer here:
why abs() function in dart return negative number when not wrapped in parenthesis?
(1 answer)
Closed 12 months ago.
I have an extension like;
extension x on num{}
and this extension contains below function;
double get wP {
assert(this >= 0.0, "value.wP: value can't be lower than 0.0!");
assert(this <= 1.0, "value.wP: value can't be bigger than 1.0!");
return (this.abs() * SizeService.instance.width).abs();}
SizeService.instance.width is a integer and it is = 50.
So, why -1.0.wp returning -50 ?
and I wan't to block all negative variable like;
-0.0 too but if I write assert like
assert(!this.isNegative, "Error bla bla");
it is not catching the negative value :(
so my question is here;
how can I block all negative and nan variables or if I can't do it, how can I convert all negative variables to positive ones ?
-0.0 is too.
because this.abs() is not working :/
thank u very much for any helpful answer!

-1.0.wP is in fact the same as -(1.0.wP)
Instead, try with (-1.0).wP

Related

In Dart, how do you set the number of decimals in a double variable? [duplicate]

This question already has answers here:
How do you round a double in Dart to a given degree of precision AFTER the decimal point?
(28 answers)
Closed last year.
I want to set a double, let's call it Price, in Dart, so that it always gives me a double of 2 decimal places.
So 2.5 would return 2.50 and 2.50138263 would also return 2.50.
The simplest answer would be double's built-in toStringAsFixed.
In your case
double x = 2.5;
print('${x.toStringAsFixed(2)}');
x = 2.50138263;
print('${x.toStringAsFixed(2)}');
Would both return 2.50. Be aware that this truncates (e.g., 2.519 returns 2.51). It does not use the standard rounding (half-even) banker's algorithm.
I recommend using a NumberFormat from the intl package; The parsing and formatting rules are worth learning since they appear in other languages like Java.
double d = 2.519;
String s = NumberFormat.currency().format(d);
print(s);
returns USD2.52
s = NumberFormat('#.00').format(d);
returns 2.52
Since your are dealing with money, you should probably use NumberFormat.currency, which would add the currency symbol for the current locale.
Your question is more about how Dart handles the type double. Something like the following might work depending on your use-case:
void main() {
double num = 2.50138263;
num = double.parse(num.toStringAsFixed(2));
print(num);
}
More info about how Dart handles double can be found here.

Why is x / 100 == 0 when x != 0 in swift? [duplicate]

This question already has answers here:
Is the Swift divide "/" operator not working or have I missed something?
(3 answers)
Division not working properly in Swift
(3 answers)
Closed 1 year ago.
I have created a for loop in which I calculate a few values.
for i in 1...100{
let xValue = i/100
print(xValue) // returns 0 every time except when i == 100
}
This is a recreation of a part of that for loop. Why is it that I do not get the right value for 'xValue'?
For info I have also tried the following:
let xValue: Float = Float(i/100)
And that doesn't work either, despite me being very specific. I must have forgotten something basic about these arithmetic
operators in swift.
When you divide an Int by an Int, the result will be rounded down. Use a floating point type, like Double or Float for more precision.
for i in 1...100 {
let xValue = Float(i)/100
print(xValue)
}
To address your attempted solution - when you do:
let xValue: Float = Float(i/100)
The Int result is first computed in i/100 (and rounded down to 0) then you are casting to a Float.
Therefore, we cast i to a Float before the division so the result is computed as a Float.
Since i and 100 are both integer values, / will do integer division and the result will be truncated to 0.
Even when you do let xValue: Float = Float(i/100), the result of division inside the parentheses is already truncated to 0 before the value can be converted to a Float.
Convert i to a floating-point value before dividing to prevent the result from being truncated.
for i in 1...100{
let xValue = Float(i)/100
print(xValue)
}

DIY rounding function gives weird values [duplicate]

This question already has answers here:
How do you round a double in Dart to a given degree of precision AFTER the decimal point?
(28 answers)
Closed 10 months ago.
I've coded this simple function to round doubles to a custom step size.
The normal .round() function retuns an int and can only rounds to the nearest 1.
My function returns a double and can round to the nearest 100.0, 5.0, 1.0, 0.1 or 0.23, you get it.
But when I put in certain doubles the result doesn't really work out and is a very tiny fraction off.
I think this has something to do with how computers do floating comma calcualations, but I need an efficient way to get around it.
Run on DartPad
void main() {
stepround(61.337551616741315, 0.1); // this should be 61.3 but is 61.300000000000004
}
/// rounds a double with given steps/precision
double stepround(double value, double steps) {
double rounded = (value / steps).round() * steps;
print(value.toString() + " rounded to the nearest " + steps.toString() + " is " + rounded.toString());
return rounded;
}
As mentioned in the comments, the cause of this issue the way that computers deal with floating numbers. Please refer to the links in the comments for further explanation.
However in a nutshell the problem is mostly caused when dividing or multiplying decimals with decimals. Therefore we can create a similar method to the one you created but with a different approach. We we will take the precision to be as an int.
I.e: 0.1 => 10; 0.001 => 1000
double stepround(double value, int place){
return (value * place).round() / place;
}
Example
// This will return 61.3
stepround(61.337551616741315, 10);
// This will return 61.34
stepround(61.337551616741315, 100);
// This will return 61.338
stepround(61.337551616741315, 1000);
This method works since the small fraction that is caused by the multiplication is removed by round(). And after that we are doing a division by an integer which doesn't create such a problem.

Why 1.001298642*10^9 == 1001298642 is returning false? [duplicate]

This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Is floating point math broken?
(31 answers)
Closed 3 years ago.
While I was working on Collatz Conjecture, I seen that there was a starting point 1.001298642*10^9 where I were getting 1047 iterations to reach 1. And against these iterations my matlab programming was returning 1001298642 in a string which is 1.001298642*10^9. But when I further work and tested on Matlab
>> 1.001298642*10^9 == 1001298642
ans =
logical
0
which means 1.001298642*10^9 is not equal to 1001298642. But actually both values are same.
I also tested these values on R Studio and same result I got. What is the problem. Am I doing some mistake?
You are using the wrong operator here. ^ is Bitwise XOR. You should be using ** for Exponentiation opeartor
Note that, this will return false because 1.001298642 * 10 ** 9 returns 1001298642.0000001 and not 1001298642 (More on that here: Is floating point math broken?)
console.log(1.001298642 * 10 ** 9 == 1001298642)
console.log(1.001298642 * 10 ** 9)
But, it is interesting that the snippet returns true. 1.001298642*10^9 == 1001298642 enters the if block because the expression returns 10 which is a truthy value:
console.log(1.001298642*10^9 == 1001298642)
In the Operator precedence table, Equality operator(==) appears before Bitwise XOR (^). If you check the the expression in AST Explorer, it is grouped like this:
(1.001298642*10)^(9 == 1001298642)
10.01298642 ^ false
10.01298642 ^ 0 // coerced to zero

Figuring out why percentages always returns 0 [duplicate]

This question already has answers here:
Math divison in Swift
(4 answers)
Closed 3 years ago.
I am trying to figure out the percentages like so:
let percentages:CGFloat = CGFloat((result.count / 100) * 100)
But this always returns 0. What am I doing wrong?
result.count is 2.
The problem is that you're probably performing an integer division, so you need to convert that count to something else first:
let percentages = CGFloat(result.count) / 100 * 100
Notice, however that you're dividing and multiplying by the same value (100). You might need to tweak that too to achieve the desired result.