Why is Integer.MIN_VALUE seen as a double by nashorn? - java-8

I pass Integer.MIN_VALUE in some Javascript that just returns the input. However, it seems that when I compare the result of the Javascript by using ScriptValueConverter.unwrapValue(returnedObject), the value is the same but the type is a double while I am expecting an integer.
However when I pass Integer.MAX_VALUE in, I do get an integer.
From what I understand, Nashorn uses optimistic type casting so in my eyes, MIN_VALUE can still be an int, there's no need for it to be a double

I suppose that the problem is not Nashorn itself but the javascript code you are running or the third party converter you are using (ScriptValueConverter is neither a javax.script nor a jdk.nashorn class).
I've run the following code:
ScriptEngine jsEngine = new ScriptEngineManager().getEngineByName("nashorn");
jsEngine.eval("function identity(x) {return x;}");
System.out.println("Integer.MIN_VALUE:" + ((Invocable) jsEngine).invokeFunction("identity", Integer.MIN_VALUE).getClass());
System.out.println("Integer.MAX_VALUE:" +((Invocable) jsEngine).invokeFunction("identity", Integer.MAX_VALUE).getClass());
and the output is:
Integer.MIN_VALUE: class java.lang.Integer
Integer.MAX_VALUE: class java.lang.Integer
Here you can find a brief explanation about nashorn type conversions.

Related

A non well formed numeric value encountered in laravel

I am trying to add two variables together. I believe both contain an integer, but when I draw what is stored within $product->mileage, I receive the following error:
A non well formed numeric value encountered
$oilchange = $request->oilchange_at_kms;
$product = Product::find($request->product_id);
$mileage = $product->mileage; // Error within this variable, but it is an int
$total = $mileage + $oilchange;
How can I test this, or how can I find the problem in my code?
This error usually pops up when you try to add an integer with a string or some type of non numeric field.
You can test this by using the PHP gettype() method:
dump(gettype($product->mileage));
dd(gettype($oilchange));
If it turns out that one of these is a string (possibly from a form response), you can cast it to an int if you are certain that the value will always be an int.
$mileage = (int)$product->mileage;
Not really recommending this, as you should try to resolve the types within the variables first, but it may help you in testing.

How does one convert Local<Name> to a string?

So after being away awhile some things changed. I used to be able to do this:
void ObjectTemplateProxy::GetProperty(Local<String> hName, const PropertyCallbackInfo<Value>& info)
{
auto hStr = hName->ToString();
But now I need an isolate, and the parameter type changed to Local<Name>. Then I tried this:
auto hStr = hName->ToString(info.GetIsolate());
But this still doesn't work because hStr is null. It is null because, as it turns out, the name is actually a Symbol type and NOT String. I don't see any way to convert a symbol to a string, which I need to do to send the name to the C# CLR via P/Invoke to pull the value from a dictionary using string keys.
So, it would seem that there is a Name() function on the Symbol type that returns a String. I was looking for a function on the Symbol type and didn't find one (must have missed it). The code that worked was hName.As<Symbol>()->Name().As<String>();.
As noted below in the comments, Symbol() is not required to have a name, so beware!

Converting a string to double in Dart using double.parse()

I am trying to convert coordinates returned from the Google Geo. API into doubles. A simplified part of the response that gets returned is...
{
locationData: {
bounds: {
northeast: 40.222222,
southwest: 38.265987
}
}
}
When I use print(locationData['bounds']['northeast']) my console reads that and understands that the value is 40.222222 However, when I try to use a parse method, I get the following error:
var neLat = double.parse(locationData['bounds']['northeast']);
I/flutter (10537): type 'double' is not a subtype of type 'String' in type cast where
I/flutter (10537): double is from dart:core
I/flutter (10537): String is from dart:core
That error leads me to believe that my parse is not valid, even though it is clearly a double value, just as a string. Am I missing something for this conversion? I have seen conversion in multiple examples using double.parse, I just can't figure out what my issue is..
If the value was parsed from JSON, Dart knows the value is a double and the error is telling me that the value is of type double and the convert expected type string.
So the error is much more clear once I understood!
I was too facing same issue.
I know it is a silly answer but it worked for me. I was using Getx to save states and while converting string to double it was displaying error
String is not subtype of type double
as the state of previous variables was being saved, so I restarted the app and tried type casting and it worked well.

string was not recognized as a valid datetime. in c#

i am using this code for passing datetime.
Convert.ToDateTime(textBox1.Text)
but the compiler show an error that string was not recognized as a valid datetime. so how to avoid this error and is a datetime field in my database.
Convert.ToDateTime(textBox1.Text)
This code will throw an exception if the string value in textBox1.Text doesn't represent a valid DateTime (or at least can't be parsed into one with default functionality in C#). You can add some defensive programming to handle errors like this.
The DateTime type (as well as most, if not all, common value types in .NET) has a method on it called TryParse() specifically for the purpose to attempting to parse a value into that type without throwing an exception. The method returns true if the parse was successful, false otherwise. And it accepts an out parameter to hold the resulting parsed value (or the original value if parsing is unsuccessful).
So instead of this:
var dateTimeValue = Convert.ToDateTime(textBox1.Text);
You could use something like this:
var dateTimeValue = DateTime.MinValue;
if (DateTime.TryParse(textBox1.Text, out dateTimeValue))
// use the value for something

Using Generic with Func as a parameter

My code is simply:
public override C Calculator<C>(Team[] teams, Func<Team, C> calculatorFunc)
{
return teams.Average(calculatorFunc);
}
I get this error:
Error 2 The type arguments for method 'System.Linq.Enumerable.Average(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
How can I fix this?
You can't - at least in the current form. There is no Average overload available that works on completely generic values (i.e. for all types C as you specified).
Average needs lists of numbers (int, double, float ...) or a conversion function that produces numbers. In the current form, you could call Calculator<string> and it would make absolutely no sense to compute the average of strings.
You'll just have to restrict the method to a specific numeric type (or provide overloads), but generics simply won't work.
The Enumerable.Average method does not have an overload which works on a generic type. You're trying to call Average<TSource>(IEnumerable<TSource>, Func<TSource, C>), which does not exist.
In order to use average, you'll need to specify one of the types (for C) that actually exists, such as double, decimal, etc.
Instead of writing:
Calculate(team, calcFunc);
You will have to write:
Calculate<MyClass>(team, calcFunc);
However, you really should know what calculatorFunc is returning --- I'm going to assume that all of the ones you use return the same value type (whether it be decimal or int of float). In which case, you could define it as:
public override int Calculator(Team[] teams, Func<Team, int> calculatorFunc)
{
return teams.Average(calculatorFunc);
}
Then you have no generics in the declaration at all to worry about.

Resources