0

I am trying to make an iOS app that will calculate pi to a set about of decimal places based on what the user wants. I currently have a python script that will take in an integer value from the user and then use that in the calculation of Pi. I also have a swift/Xcode project that will only allow a user to input an integer value. So I am wondering if there is a way for me to pass this value that the user enters from a textbox in my swift project to then give that to the python code, run it, and then output the result? In my python code, I am using "timer" to be able to calculate the time it takes to calculate pi to the set digits that was requested. So I would only need to be able to display this result.

I have thought about rewriting the python script into swift, but I am unsure of how to do that. This is the definition I am using in my python script to calculate pi:

def pi():
decimal.getcontext().prec += 2
three = decimal.Decimal(3)
first, second, third, forth, fifth, sixth, numb = 0, three, 1, 0, 0, 24, 3 
while numb != first:
    first = numb
    third, forth = third + forth, forth + 8
    fifth, sixth = fifth + sixth, sixth + 32
    second = (second * third) / fifth
    numb += second
decimal.getcontext().prec -= 2
return +numb

But I was unsure of how to rewrite this into swift language, so I figured getting swift to pass the precision value into python would be easier.

3
  • It's probably easier to rewrite the scrip in swift. Can you even execute python scripts in iOS? Commented Jul 3, 2019 at 17:24
  • @JoakimDanielson I figured that if it was able to be executed by swift then it wouldn't make a difference if iOS can natively run them or not. I know that if I was to download an app I could run a python ide or python notebook on my device but I thought that if a python code was referenced by swift then it wouldn't cause much compatibility issues for this simple project. Commented Jul 3, 2019 at 17:29
  • Swift doesn't execute anything by itself, you will need to have Python installed. Why not learn a little swift and re-write your code, it's your only option I believe? Commented Jul 3, 2019 at 18:26

1 Answer 1

1

You definitely can run Python code in Swift since Python is designed to be embedded and has a C interface API. Check how Swift for TensorFlow uses Python interoperability (although I couldn't find a quick way to only use that module and not the whole TensorFlow). You can also check PythonKit out.

However, I don't think rewriting that script would be too difficult, and it might be better to avoid more libraries and dependencies in your project.

Edit: As Joakim Danielson pointed out, you'll need the Python runtime, and it doesn't seem to be available in iOS, so you seem to be limited to macOS for this.

Sign up to request clarification or add additional context in comments.

4 Comments

You still need a python runtime, where do you have that on iOS?
My bad, didn't see the iOS tag.
@RenzoTissoni i was thinking about rewriting it in swift language, however I am currently unaware of swifts version of Decimal and being able to set the precision of said variable like you can in python. I know currently if I do it in Swift, I might be limited to only 15 place precision by default, that is unless I am unaware of a package/variable type that would allow me a varied precision based on users input.
Do you really need it? There are many way of calculating π to a certain number of digits, so why settling for the way you were using?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.