Skip to content

Conversation

@BCSharp
Copy link
Member

@BCSharp BCSharp commented Mar 31, 2022

A thought: move TryToFloat to PythonOps?

@BCSharp BCSharp marked this pull request as draft March 31, 2022 05:19
@slozier
Copy link
Contributor

slozier commented Mar 31, 2022

A thought: move TryToFloat to PythonOps?

Could be an idea but we should probably check if it would be useful outside of the float/complex constructors. For example, in the constructors __float__ takes precedence, but in method calls it doesn't:

class myfloat(float):
    def __float__(self):
        return 1.0

assert float(myfloat(0.0)) == 1.0
assert abs(myfloat(0.0)) == 0.0

} else if (value is int i) {
result = i;
} else if (value is BigInteger bi) {
result = (double)bi;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess these cast is where testing fails. The BigInteger to double cast returns infinity instead of overflowing. There's a MathUtils.ToFloat64 that throws (but we should consider a helper at the ipy level so we can match the error message).

Copy link
Member Author

@BCSharp BCSharp Apr 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. The fast track just cut too many corners. __float__ and __complex__ on int do it correctly, with a call to MathUtils.ToFloat64. Funny thing, I started working on complex.__new__ as a spin-off from an investigation whether int.__complex__ can be safely removed (it can, assuming that complex.__new__ works correctly) and then completely forgot about that.

I like the idea of our own helper class with a Python-specific error message.

@BCSharp
Copy link
Member Author

BCSharp commented Apr 1, 2022

I looked into the CPython's code and roughly speaking the functionality of TryToFloat resides inside PyNumber_Float, except that the latter also does string parsing, so I suppose is more or less equivalent to NewFloat. PyNumber_Float is used in a few places but it seems that apart from of the float and complex constructors, only to parse a string. So I suppose no need to move TryToFloat to PythonOps.

@BCSharp BCSharp marked this pull request as ready for review April 3, 2022 00:16
Copy link
Contributor

@slozier slozier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@BCSharp BCSharp merged commit 4eee36e into IronLanguages:master Apr 3, 2022
@BCSharp BCSharp deleted the complex__new__ branch April 3, 2022 04:22
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

Successfully merging this pull request may close these issues.

2 participants