Iam new to dart I want a Make table of food program which take an input from user for each food and how much they want but i have an problem with multiply listOfFood[name] with count which both of them double is there any way to solve this ?
double name , count ;
Map<String , dynamic> listOfFood = {
'bacon' :'4.2',
'salad' :'3.5',
'cheaken' :'5.6',
'Goatmeat' :'6.9',
'fish' : '6.5',
};
Map<int , dynamic> food = {
1 : listOfFood ['bacon']
,2 : listOfFood ['salad']
,3 : listOfFood ['cheaken']
,4 : listOfFood ['Goatmeat']
,5 : listOfFood ['fish']
};
stdout.write('please Choose your product \n ${listOfFood}');
name = double.parse(stdin.readLineSync()!);
count = double.parse(stdin.readLineSync()!);
double result = (food[name]) * count;
print(result);
}
You cannot coerce a String that looks like a number to a double in Dart.
Dart has strong typing even when you declare a variable dynamic.
In your case, you're taking the value from listOfFood (not a List, use a better name like priceByFoodName), which is always a String (despite the Map having dynamic values) and multiplying that with a double which can't work.
You should ask yourself why are the prices Strings?
If you want to use them as numbers, just make them numbers.
Also, you don't need dynamic at all. Use types! If you had done that the compiler would've told you right away what the problem was before you even ran the code. That's why types exist.
To solve the problem quickly... this line:
double result = (food[name]) * count;
Should be changed to:
double result = double.parse(food[name]!) * count;
But you should probably change your code quite a bit to handle errors, use appropriate types, stop ignoring nulls etc.
Related
weight is a field (Number in Firestore), set as 100.
int weight = json['weight'];
double weight = json['weight'];
int weight works fine, returns 100 as expected, but double weight crashes (Object.noSuchMethod exception) rather than returning 100.0, which is what I expected.
However, the following works:
num weight = json['weight'];
num.toDouble();
When parsing 100 from Firestore (which actually does not support a "number type", but converts it), it will by standard be parsed to an int.
Dart does not automatically "smartly" cast those types. In fact, you cannot cast an int to a double, which is the problem you are facing. If it were possible, your code would just work fine.
Parsing
Instead, you can parse it yourself:
double weight = json['weight'].toDouble();
Casting
What also works, is parsing the JSON to a num and then assigning it to a double, which will cast num to double.
double weight = json['weight'] as num;
This seems a bit odd at first and in fact the Dart Analysis tool (which is e.g. built in into the Dart plugin for VS Code and IntelliJ) will mark it as an "unnecessary cast", which it is not.
double a = 100; // this will not compile
double b = 100 as num; // this will compile, but is still marked as an "unnecessary cast"
double b = 100 as num compiles because num is the super class of double and Dart casts super to sub types even without explicit casts.
An explicit cast would be the follwing:
double a = 100 as double; // does not compile because int is not the super class of double
double b = (100 as num) as double; // compiles, you can also omit the double cast
Here is a nice read about "Types and casting in Dart".
Explanation
What happened to you is the following:
double weight;
weight = 100; // cannot compile because 100 is considered an int
// is the same as
weight = 100 as double; // which cannot work as I explained above
// Dart adds those casts automatically
You can do it in one line:
double weight = (json['weight'] as num).toDouble();
You can Parse the data Like given below:
Here document is a Map<String,dynamic>
double opening = double.tryParse(document['opening'].toString());
In Dart, int and double are separate types, both subtypes of num.
There is no automatic conversion between number types. If you write:
num n = 100;
double d = n;
you will get a run-time error. Dart's static type system allows unsafe down-casts, so the unsafe assignment of n to d (unsafe because not all num values are double values) is treated implicitly as:
num n = 100;
double d = n as double;
The as double checks that the value is actually a double (or null), and throws if it isn't. If that check succeeds, then it can safely assign the value to d since it is known to match the variable's type.
That's what's happening here. The actual value of json['weight'] (likely with static type Object or dynamic) is the int object with value 100. Assigning that to int works. Assigning it to num works. Assigning it to double throws.
The Dart JSON parser parses numbers as integers if they have no decimal or exponent parts (0.0 is a double, 0e0 is a double, 0 is an integer). That's very convenient in most cases, but occasionally annoying in cases like yours where you want a double, but the code creating the JSON didn't write it as a double.
In cases like that, you just have to write .toDouble() on the values when you extract them. That's a no-op on actual doubles.
As a side note, Dart compiled to JavaScript represents all numbers as the JavaScript Number type, which means that all numbers are doubles. In JS compiled code, all integers can be assigned to double without conversion. That will not work when the code is run on a non-JS implementation, like Flutter, Dart VM/server or ahead-of-time compilation for iOS, so don't depend on it, or your code will not be portable.
Simply convert int to double like this
int a = 10;
double b = a + 0.0;
If I have a square grid (created in WPF/XAML) in any size, with a given number of cells per side, it should be ridiculously easy to calculate the coordinates to a cell that was clicked by getting the mouse cursor position relative to the grid element. As I try to learn F#, I experience some problems with the syntax and would appreciate some input.
Here is an example of what I am trying to do:
// From a mouse event, fetch the position of the cursor relative to a IInputElement
let get_mouse_position (relative_to : IInputElement) (args : Input.MouseEventArgs) : (int*int) =
let position = args.GetPosition relative_to
((Convert.ToInt32(position.X) / cellsPerSide), (Convert.ToInt32(position.Y) / cellsPerSide))
// Get the position of the cursor relative to the grid
let get_cell_coordinates (element : IInputElement) =
get_mouse_position element
However, when I try to use the coordinates retrieved by calling get_cell_coordinates somewhere else where (x,y) coordinates are needed, I get an error that says:
This expression was expected to have type int * int but here has type 'a -> int * int
So, what am I doing wrong and why do I get this polymorph type and not just a tuple of integers?
The type you got is not a "polymorph" type, it's a function type. The reason you got a function of type 'a -> int * int instead of the int * int result you were expecting is because you didn't pass all the parameters to your function, so F# returned a function that expected the rest of the parameters. This is called "partial application", and you can read more about it here:
https://fsharpforfunandprofit.com/posts/currying/
and
https://fsharpforfunandprofit.com/posts/partial-application/
A quick summary of the two articles: in F#, all functions are treated as taking one parameter and returning one result. Yes, ALL functions. When you create a function that appears to take two parameters, F# rewrites it internally. It becomes a function that takes one parameter and returns a second function; this second function takes one parameter and returns the result. A concrete example will probably be useful at this point. Consider this function:
let doubleAndSubtract a b = (a * 2) - b
(Obviously, the parentheses around a * 2 aren't actually needed, but I left them in to make the function unambiguous to read).
Internally, F# actually rewrites this function into the following:
let doubleAndSubtract a =
let subtract b = (a * 2) - b
subtract
In other words, it builds a function that "closes over" (captures) the value of a that you passed in. So the following two functions are completely equivalent:
let explicitSubtract b = (5 * 2) - b
let implicitSubtract = doubleAndSubtract 5
If you type these functions in to the F# interactive prompt and look at the types that it declares each function to have, explicitSubtract has type b:int -> int, and implicitSubtract has type int -> int. The only difference between these two is that the type of explicitSubtract names the parameter, whereas implicitSubtract doesn't "know" the name of its parameter. But both will take an int, and return an int (specifically, 10 minus the parameter).
Now, let's look at the type signature for doubleAndSubtract. If you typed it into the F# interactive prompt, you'll have seen that its type signature is int -> int -> int. In type signatures, -> is right-associative, so that signature is really int -> (int -> int) -- in other words, a function that takes an int and returns a function that takes an int and returns an int. But you can also think of it as a function that takes two ints and returns an int, and you can define it that way.
So, what happened with your code here is that you defined the function like this:
let get_mouse_position (relative_to : IInputElement) (args : Input.MouseEventArgs) : (int*int) = ...
This is a function that takes two parameters (of types IInputElement and Input.MouseEventArgs) and returns a tuple of ints... but that's equivalent to a function that takes one parameter of type IInputElement, and returns a function that takes an Input.MouseEventArgs and returns a tuple of two ints. In other words, your function signature is:
IInputElement -> Input.MouseEventArgs -> (int * int)
When you called it as get_mouse_position element you passed it only a single parameter, so what you got was a function of type Input.MouseEventArgs -> (int * int). The type checker reported this as type 'a -> int * int (changing Input.MouseEventArgs into the generic type name 'a) for reasons I don't want to get into here (this is already rather long), but that's why you got the result you got.
I hope this helps you understand F# functions better. And do read those two articles I linked to; they'll take your understanding another step further.
I solved it by using the static Mouse.GetPosition method to obtain the position of the mouse instead of Input.MouseEventArgs.
The code now looks as follows, if anyone else has the same problem:
// From a mouse event, fetch the position of the cursor relative to a IInputElement
let get_mouse_position (relative_to : IInputElement) : (int*int) =
let position = Mouse.GetPosition relative_to
((Convert.ToInt32(position.X) / cellsPerSide), (Convert.ToInt32(position.Y) / 32))
// Get the position of the cursor relative to the input element
let get_cell_coordinates (element : IInputElement) =
get_mouse_position element
Very simple issue. I have the useless class:
class Useless{
double field;
Useless(this.field);
}
I then commit the mortal sin and call new Useless(0);
In checked mode (which is how I run my tests) that blows up, because 'int' is not a subtype of type 'double'.
Now, it works if I use new Useless(0.0) , but honestly I spend a lot of time correcting my tests putting .0s everywhere and I feel pretty dumb doing that.
As a temporary measure I rewrote the constructor as:
class Useless{
double field;
Useless(num input){
field = input.toDouble();
}
}
But that's ugly and I am afraid slow if called often. Is there a better way to do this?
Simply toDouble()
Example:
int intVar = 5;
double doubleVar = intVar.toDouble();
Thanks to #jamesdlin who actually gave this answer in a comment to my previous answer...
In Dart 2.1, integer literals may be directly used where double is expected. (See https://github.com/dart-lang/sdk/issues/34355.)
Note that this is syntactic sugar and applies only to literals. int variables still won't be automatically promoted to double, so code like:
double reciprocal(double d) => 1 / d;
int x = 42;
reciprocal(x);
would fail, and you'd need to do:
reciprocal(x.toDouble());
You can also use:
int x = 15;
double y = x + .0;
use toDouble() method.
For e.g.:
int a = 10
print(a.toDouble)
//or store value in a variable and then use
double convertedValue = a.toDouble()
From this attempt:
class Useless{
double field;
Useless(num input){
field = input.toDouble();
}
}
You can use the parse method of the double class which takes in a string.
class Useless{
double field;
Useless(num input){
field = double.parse(input.toString()); //modified line
}
}
A more compact way of writing the above class using constructor's initialisers is:
class Useless{
double _field;
Useless(double field):_field=double.parse(field.toString());
}
Since all divisions in flutter result to a double, the easiest thing I did to achieve this was just to divide the integer value with 1:
i.e.
int x = 15;
double y = x /1;
There's no better way to do this than the options you included :(
I get bitten by this lots too, for some reason I don't get any warnings in the editor and it just fails at runtime; mighty annoying :(
I'm using a combination:
static double checkDouble(dynamic value) {
if (value is String) {
return double.parse(value);
} else if (value is int) {
return 0.0 + value;
} else {
return value;
}
}
This is how you can cast from int to double
int a = 2;
double b = a*1.0;
I'm working with multiplications in my objective c application. The problem is, when i do some multiplications, sometimes it gives me a false result :
Exemple :
int intern = 77986;
int secondNumber = 70654;
long long int multiply = intern * secondNumber;
NSLog(#"Intern : %i two : %i mult : %lli", intern, secondNumber, multiply);
The result is :
Intern : 77986 two : 70654 mult : 1215055548
The result would be 5510022844
I don't understand... Thank you for your help !
It's a simple overflow issue. An operation with two int variables is done as an int. But the result is too big for int so it overflows. Then the overflowed result is assigned to the long long int.
Making either (or both) of the first two int variables into long long int will result in the operation being done as a long long int resulting in the expected and correct result.
To clarify, you can either do:
long long int intern = 77986;
// and the rest
or do:
long long int multiply = (long long int)intern * secondNumber;
125055548 -> 0x 486C46BC
5510022844 -> 0x1486C46BC
Since you're multiplying ints, the result is an int, and gets truncated down to 32bits.
How to get the length of a string array like
str 30 name[];//dynamic array
I used the following for getting the length,but it showing the error as "the variable is not of the type CLASS."
int len=name.get_length();
It sounds like you might be happier using the Array collection class.
http://msdn.microsoft.com/en-us/library/array.aspx
static void TestArray(Args _args)
{
Array strArray = new Array(Types::String);
;
strArray.value(1, 'abc');
strArray.value(2, 'def');
info(strfmt("%1", strArray.lastIndex()));
}
You need the dimOf function. Take a look to the reference:
http://msdn.microsoft.com/en-us/library/aa597117.aspx
Sorry, there is no build-in function to return the string array size. Since you are in full control what you put in the array, there need not be any!
The built-in function dimof returns the allocated size of the array, which is only of practical value for a fixed size array like str 30 name[20], where dimof(name) returns 20.
A clean way to remain in control, is to use a setter function:
static void TestArray(Args _args)
{
str 30 name[];
int n = 0;
int i;
void nameSet(int _i, str 30 _name)
{
n = max(n,_i);
name[_i] = _name;
}
;
nameSet(2,'abc');
nameSet(4,'def');
for (i = 1; i <= n; i++)
info(name[i]);
}
There is no upper bound index limit, so accessing name[7] is perfectly valid and in this case returns a blank value. This may be used to your advantage, if you always use all holes and never stores a blank:
static void TestArray(Args _args)
{
str 30 name[];
int i;
name[1] = 'abc';
name[2] = 'def';
for (i = 1; name[i]; i++)
info(name[i]);
}
Beware that accessing a higher index (in this case higher than 2) may in fact increase the allocated size of the array.