# Small bug in "Python Fortran Rosetta Stone"

**URL:** <https://fortran-lang.discourse.group/t/small-bug-in-python-fortran-rosetta-stone/8582>\
**Category:** Uncategorized\
**Created:** [September 8, 2024, 3:59am UTC](https://fortran-lang.discourse.group/t/small-bug-in-python-fortran-rosetta-stone/8582 "2024-09-08T03:59:55Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![yydx](https://avatars.discourse-cdn.com/v4/letter/y/c67d28/32.png) [@yydx](https://fortran-lang.discourse.group/u/yydx)\
**Post date:** [September 8, 2024, 3:59am UTC](https://fortran-lang.discourse.group/t/small-bug-in-python-fortran-rosetta-stone/8582/1 "2024-09-08T03:59:55Z")

</div>

![图片](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/5/5171b4cc4a9ba635f0266c65951e523582d2eb04.png)

The result in Python cell, in line 4  
f = float(1 / 2) # 0.0  
seems wrong in Python 3.11, numpy 1.26.4. It should be 0.5.  
And I also tried float32(1/2), it returns the same result.

---

<div class="post-metadata">

**Author:** ![yydx](https://avatars.discourse-cdn.com/v4/letter/y/c67d28/32.png) [@yydx](https://fortran-lang.discourse.group/u/yydx)\
**Post date:** [September 8, 2024, 4:01am UTC](https://fortran-lang.discourse.group/t/small-bug-in-python-fortran-rosetta-stone/8582/2 "2024-09-08T04:01:34Z")

</div>

The page link is

> **[Python Fortran Rosetta Stone — Fortran Programming Language](https://fortran-lang.org/learn/rosetta_stone/)**
>
> Fortran : High-performance parallel programming language

---

<div class="post-metadata">

**Author:** ![nbehrnd](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nbehrnd](https://fortran-lang.discourse.group/u/nbehrnd)\
**Post date:** [September 8, 2024, 7:34am UTC](https://fortran-lang.discourse.group/t/small-bug-in-python-fortran-rosetta-stone/8582/3 "2024-09-08T07:34:45Z")

</div>

Go ahead, read about the rules how to contribute ([link](https://fortran-lang.org/en/community/contributing/)), fork the website’s [GitHub repository](https://github.com/fortran-lang/webpage). After edit of line 1043 of the markdown file in question (i.e. `webpage/source/learn/rosetta_stone.md`), file a PR. After passing the review, the credit will be yours.

---

<div class="post-metadata">

**Author:** ![yydx](https://avatars.discourse-cdn.com/v4/letter/y/c67d28/32.png) [@yydx](https://fortran-lang.discourse.group/u/yydx)\
**Post date:** [September 8, 2024, 11:30am UTC](https://fortran-lang.discourse.group/t/small-bug-in-python-fortran-rosetta-stone/8582/4 "2024-09-08T11:30:44Z")

</div>

Thank you very much, I will try! Have a nice day.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [September 8, 2024, 12:42pm UTC](https://fortran-lang.discourse.group/t/small-bug-in-python-fortran-rosetta-stone/8582/5 "2024-09-08T12:42:45Z")

</div>

> [@yydx](#):
>
> f = float(1 / 2) # 0.0  
> seems wrong in Python 3.11

Indeed, in Fortran (and C) the compiler chooses implicitly either to use the FPU or the integer circuit depending on the types of the numerator and denominator.

In Python, the user is explicitly choosing real `/` or euclidean division `//` :

```python
>>> 1/2
0.5
>>> 1//2
0

```

---

<div class="post-metadata">

**Author:** ![yydx](https://avatars.discourse-cdn.com/v4/letter/y/c67d28/32.png) [@yydx](https://fortran-lang.discourse.group/u/yydx)\
**Post date:** [September 8, 2024, 12:53pm UTC](https://fortran-lang.discourse.group/t/small-bug-in-python-fortran-rosetta-stone/8582/6 "2024-09-08T12:53:20Z")

</div>

Yes, thank you for your emphasis. Do you think it should be change to  
f = float(1 // 2) # 0.0  
or  
f = float(1 / 2) # 0.5

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [September 8, 2024, 1:02pm UTC](https://fortran-lang.discourse.group/t/small-bug-in-python-fortran-rosetta-stone/8582/7 "2024-09-08T13:02:32Z")

</div>

Good question…  
`f = 1 / 2 ! 0.0` is demonstrating what I said about Fortran.

So I would indeed write `f = float(1 // 2) # 0.0` in Python and would add a paragraph explaining how the Fortran compiler chooses real or euclidean division (two integers =\> euclidean division, at least one real =\> real division). Because for people knowing only one language and switching to the other, the different behaviour can be surprising. And it could create a severe bug (a program running but giving a bad result) in a scientific computation if you ignore the rules.

---

<div class="post-metadata">

**Author:** ![yydx](https://avatars.discourse-cdn.com/v4/letter/y/c67d28/32.png) [@yydx](https://fortran-lang.discourse.group/u/yydx)\
**Post date:** [September 9, 2024, 5:47am UTC](https://fortran-lang.discourse.group/t/small-bug-in-python-fortran-rosetta-stone/8582/8 "2024-09-09T05:47:02Z")

</div>

Thank you for your detailed explanation. Looking forward to seeing these on the webpage.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [September 9, 2024, 5:46pm UTC](https://fortran-lang.discourse.group/t/small-bug-in-python-fortran-rosetta-stone/8582/9 "2024-09-09T17:46:12Z")

</div>

Yes, I wrote it when Python 2 was the most used Python and Python 3 wasn’t widely used. @yydx, if you could fix it, that would be highly appreciated!

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [September 9, 2024, 6:39pm UTC](https://fortran-lang.discourse.group/t/small-bug-in-python-fortran-rosetta-stone/8582/10 "2024-09-09T18:39:47Z")

</div>

Ah yes, I did not remember that intrepid change between Python 2 and 3.

> <https://stackoverflow.com/questions/21316968/integer-division-in-python-2-and-python-3>

Should we hope that there won’t be Python 4? 🫣

---

<div class="post-metadata">

**Author:** ![nbehrnd](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nbehrnd](https://fortran-lang.discourse.group/u/nbehrnd)\
**Post date:** [September 10, 2024, 11:40am UTC](https://fortran-lang.discourse.group/t/small-bug-in-python-fortran-rosetta-stone/8582/11 "2024-09-10T11:40:38Z")

</div>

> [@vmagnin](#):
>
> Should we hope that there won’t be Python 4?

The second time Lex Fridman invited Guido van Rossum for a talk (episode 341 _Python and the Future of Programming_, recorded November 26, 2022, [link to youtube](https://www.youtube.com/watch?v=-DVyjdw4t9I)), the pros and cons of such a transition and what would be organized differently (intent to be «better this time™») were addressed (about 2h22mn into the video).

If there were a Python 4.0, then with less/or better no breaks at all about how to _access_ the modules of the _standard library_ (I presume van Rossum refers to instances like `print` statement vs `print()` function). If so, for instance because usage of no-GIL Python already became so much more prominent than the of GIL Python, with an announce multiple years ahead to provide the many third party applications to identify and test a better working – for the reason many of them (as compiled in directories like PyPI) struggle to stay afloat/maintained already today.

And now, the assisting [2-to-3](https://docs.python.org/3/library/2to3.html) for a rule-based automatic (and eventually faster) transition\* already is scheduled to disappear from the standard library with Python 3.13 (after a release candidate in August, the official shipment is anticipated for October 2024).

\* Though it helps with core syntax, subsequent changes e.g., in numpy’s array definitions are beyond its scope (by recollection, pylint and likely ruff spotted then).

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [September 10, 2024, 2:10pm UTC](https://fortran-lang.discourse.group/t/small-bug-in-python-fortran-rosetta-stone/8582/12 "2024-09-10T14:10:49Z")

</div>

The key lesson is that _if_ you are going to do breaking changes, to _allow_ the same code base to be some subset of both Python 2 and 3, so that it can run in both (unmodified, no 2to3 tool needed), thus providing a smooth, gradual transition. At the beginning the 2to3 tool was needed, which didn’t allow to develop using Python 3 directly (one had to modify the codebase using Python 2 and run the tool).
