Skip to content

Casting to Double of CGFloat.native for math functions #628

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

Closed
wants to merge 1 commit into from
Closed

Casting to Double of CGFloat.native for math functions #628

wants to merge 1 commit into from

Conversation

johnno1962
Copy link
Contributor

@johnno1962 johnno1962 commented Sep 12, 2016

Values of CGFloat.native need to be cast to Double before being passed to math functions. This PR is in preparation for the Android port of Foundation in PR #622. There is a question about whether this conversion should be necessary however as CGFloat.swift is derived from code that builds on other 32 bit platforms as is discussed in the Foundation for Android PR. It’s certainly required on Android.

@Coeur
Copy link
Contributor

Coeur commented Sep 13, 2016

By curiosity, it couldn't be CGFloat(acos(x.double)) instead of CGFloat(acos(Double(x.native)))?

[edit]
Nevermind, Parkera explained it should be like that because:

I don't like phrasing this as a type conversion hidden as a property.

@xwu
Copy link
Contributor

xwu commented Sep 13, 2016

@johnno1962, actually, this PR should not be necessary for any 32-bit platform. See:

https://github.com/apple/swift/blob/1d4a9707d6facd99b749e3bc9005e8542959ebc4/stdlib/public/Platform/tgmath.swift.gyb

This overlay renames acosf to acos, etc., so that CGFloat(acos(x.native)) is correct regardless of whether x.native is 32-bit or 64-bit. So something is wrong if the Android port won't compile, as the existing code should work.

@johnno1962
Copy link
Contributor Author

johnno1962 commented Sep 13, 2016

This is what @phausler has been saying all along. So something is wrong with my/the Android swift toolchain/stdlib? Any ideas? I followed the recipe here: https://github.com/apple/swift/blob/master/docs/Android.md.. Rebuilding from scratch...

@xwu
Copy link
Contributor

xwu commented Sep 13, 2016

Hmm. I'm not in a position to try this myself, but:

  • Can you follow that recipe and compile a "hello world"?
  • Can you follow that recipe and compile a "hello world" with "print(acos(0.5 as Float))"?

@xwu
Copy link
Contributor

xwu commented Sep 13, 2016

If both of those work but "print(acos(CGFloat(0.5 as Float).native))" doesn't, then the code might be missing an "#if os(Android)" somewhere in CGFloat or a related file; otherwise, if one of those steps doesn't work, it's might be a similar problem with Float, the tgmath overlay, or something else in that arena...

@rintaro
Copy link
Member

rintaro commented Sep 13, 2016

I think, this is because Glibc is not imported for Android.

@johnno1962
Could you try adding || os(Android) there?

Oh, you already tried that in #622. Never mind.

@rintaro
Copy link
Member

rintaro commented Sep 13, 2016

(based on #632)
But, forcing to use Float for CGFloat.NativeType works as expected on my Ubuntu.
master...rintaro:cgfloat-forcefloat
If tgmath functions don't work in 32bit environment or Android, we might be missing something.

@johnno1962
Copy link
Contributor Author

A bit of trouble with the rebuild:
/home/johnno/Swift/swift/stdlib/public/Platform/Glibc.swift:13:19: error: no such module 'SwiftGlibc'
@_exported import SwiftGlibc // Clang module
^
Had to copy glib.modulemap across from Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/linux/x86_64/glibc.modulemap, then it regenerated for Android anyway..

@johnno1962
Copy link
Contributor Author

johnno1962 commented Sep 13, 2016

A little more progress. After a complete rebuild the CGFloat.swift problems have disappeared while the NSGeometry.swift casting is still required as values are being assigned from a Float to a Double on 32 bits despite tgmath adapting to the argument type.

Something like #632 is probably required.

Foundation/NSGeometry.swift:464:40: error: cannot convert value of type 'CGFloat.NativeType' (aka 'Float') to expected argument type 'Double'
width = floor(aRect.size.width.native)
~~~~~~~~~~~~~~~~~^~~~~~
Double( )
Foundation/NSGeometry.swift:468:42: error: cannot convert value of type 'CGFloat.NativeType' (aka 'Float') to expected argument type 'Double'
height = floor(aRect.size.height.native)
~~~~~~~~~~~~~~~~~~^~~~~~
Double( )
Foundation/NSGeometry.swift:472:39: error: cannot convert value of type 'CGFloat.NativeType' (aka 'Float') to expected argument type 'Double'
width = ceil(aRect.size.width.native)
~~~~~~~~~~~~~~~~~^~~~~~
Double( )
Foundation/NSGeometry.swift:476:41: error: cannot convert value of type 'CGFloat.NativeType' (aka 'Float') to expected argument type 'Double'
height = ceil(aRect.size.height.native)
~~~~~~~~~~~~~~~~~~^~~~~~
Double( )
Foundation/NSGeometry.swift:480:40: error: cannot convert value of type 'CGFloat.NativeType' (aka 'Float') to expected argument type 'Double'
width = round(aRect.size.width.native)
~~~~~~~~~~~~~~~~~^~~~~~
Double( )
Foundation/NSGeometry.swift:484:42: error: cannot convert value of type 'CGFloat.NativeType' (aka 'Float') to expected argument type 'Double'
height = round(aRect.size.height.native)
~~~~~~~~~~~~~~~~~~^~~~~~
Double( )
Foundation/NSGeometry.swift:490:36: error: cannot convert value of type 'CGFloat.NativeType' (aka 'Float') to expected argument type 'Double'
minX = ceil(aRect.origin.x.native)
~~~~~~~~~~~~~~~^~~~~~
Double( )
Foundation/NSGeometry.swift:494:36: error: cannot convert value of type 'CGFloat.NativeType' (aka 'Float') to expected argument type 'Double'
minY = ceil(aRect.origin.y.native)
~~~~~~~~~~~~~~~^~~~~~
Double( )
Foundation/NSGeometry.swift:498:44: error: binary operator '+' cannot be applied to two 'CGFloat.NativeType' (aka 'Float') operands
maxX = floor(aRect.origin.x.native + aRect.size.width.native)
~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~
Foundation/NSGeometry.swift:498:44: note: expected an argument list of type '(Double, Double)'
maxX = floor(aRect.origin.x.native + aRect.size.width.native)
^
Foundation/NSGeometry.swift:502:44: error: binary operator '+' cannot be applied to two 'CGFloat.NativeType' (aka 'Float') operands
maxY = floor(aRect.origin.y.native + aRect.size.height.native)
~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~
Foundation/NSGeometry.swift:502:44: note: expected an argument list of type '(Double, Double)'
maxY = floor(aRect.origin.y.native + aRect.size.height.native)
^
Foundation/NSGeometry.swift:508:37: error: cannot convert value of type 'CGFloat.NativeType' (aka 'Float') to expected argument type 'Double'
minX = floor(aRect.origin.x.native)
~~~~~~~~~~~~~~~^~~~~~
Double( )
Foundation/NSGeometry.swift:512:37: error: cannot convert value of type 'CGFloat.NativeType' (aka 'Float') to expected argument type 'Double'
minY = floor(aRect.origin.y.native)
~~~~~~~~~~~~~~~^~~~~~
Double( )
Foundation/NSGeometry.swift:516:43: error: binary operator '+' cannot be applied to two 'CGFloat.NativeType' (aka 'Float') operands
maxX = ceil(aRect.origin.x.native + aRect.size.width.native)
~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~
Foundation/NSGeometry.swift:516:43: note: expected an argument list of type '(Double, Double)'
maxX = ceil(aRect.origin.x.native + aRect.size.width.native)
^
Foundation/NSGeometry.swift:520:43: error: binary operator '+' cannot be applied to two 'CGFloat.NativeType' (aka 'Float') operands
maxY = ceil(aRect.origin.y.native + aRect.size.height.native)
~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~
Foundation/NSGeometry.swift:520:43: note: expected an argument list of type '(Double, Double)'
maxY = ceil(aRect.origin.y.native + aRect.size.height.native)
^
Foundation/NSGeometry.swift:526:37: error: cannot convert value of type 'CGFloat.NativeType' (aka 'Float') to expected argument type 'Double'

@johnno1962
Copy link
Contributor Author

Closed in favour of #632 which builds for me on Android & #631. CGFloat.swift changes are not required after complete rebuild of toolchain from master.

@johnno1962 johnno1962 closed this Sep 13, 2016
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.

4 participants