Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ints aren't floats, but are floats, but aren't #379

Open
Draco18s opened this issue Nov 1, 2023 · 0 comments
Open

Ints aren't floats, but are floats, but aren't #379

Draco18s opened this issue Nov 1, 2023 · 0 comments

Comments

@Draco18s
Copy link

Draco18s commented Nov 1, 2023

xNode/Scripts/NodePort.cs

Lines 155 to 158 in 82f7887

public T GetInputValue<T>() {
object obj = GetInputValue();
return obj is T ? (T) obj : default(T);
}

What happens if GetInputValue() returns an integer and the port has a type T that is a float?

What value is returned by this method?

Answer:

The returned value is 0 (and always and only 0) because integer is not float and thus the check is False and the default value is returned. However, one would expect that an integer can be cast to a float, returning (T)obj instead.

The correct line on 157 should be return obj != null && obj.GetType().IsInstanceOfType(typeof(T)) ? (T)obj : default(T);
I get this part wrong all the time. I meant IsAssignableFrom but that doesn't actually work here either. Had to do this:

public T GetInputValue<T>() {
  object obj = GetInputValue();
  
  if (typeof(IConvertible).IsAssignableFrom(typeof(T)) && obj is IConvertible)
  {
    return (T)Convert.ChangeType(obj, typeof(T));
  }
  return obj is T ? (T)obj : default(T);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant