# Inaccurate floats in the self-referential Tupper formula

I am trying to create a Python program that displays the Tupper self-reference formula and I am facing some problems.

At first, my program couldn't handle the big floats it had to handle, so I decided to use `BigFloats`

it to sort out my problems. It worked. My problem is that I have a 540 digit number that needs to be multiplied by `BigFloat`

, and when I do that, it rounds the number, making it inaccurate, which causes problems later. I tried raising `precision`

to 1000 and it still keeps rounding the variable.

The point is that some numbers can be handled without `BigFloat`

to the exact value, but some numbers cannot be handled normally and with `BigFloat`

right now.

Here is one of the calculations below (this one can be processed without `BigFloat`

):

``````import bigfloat
y = 960939379918958884971672962127852754715004339660129306651505519271702802395266424689642842174350718121267153782770623355993237280874144307891325963941337723487857735749823926629715517173716995165232890538221612403238855866184013235585136048828693337902491454229288667081096184496091705183454067827731551705405381627380967602565625016981482083418783163849115590225610003652351370343874461848378737238198224849863465033159410054974700593138339226497249461751545728366702369745461014655997933798537483143786841806593422227898388722980000748404719
x = 0
with bigfloat.precision(1000):
(y//17 * bigfloat.pow(2,0)) % 2
```

```

This code should return 1, but it returns 0 instead.

Is there a way to make it `BigFloat`

more precise so that I can use it in my program?

+3

source to share

You don't need floating point math for Tupper's formula. The trick is that 2 -x is the same as 1/2 x so you only need to work with integers, because you don't need to calculate the floating point number 2 -x and multiply it by an integer, but instead calculate the integer 2 x and divide another integer by that integer. Applied by Tupper's formula, part 2 -17 * int (x) - int (y)% 17 becomes 1/2 17 * int (x) + int (y)% 17 .

See the following version of the Tupper function, which works entirely on the integer domain (if you don't know what it is `**`

, it's the Python Power Operator ):

``````def tuppers_formula(x, y):
"""Return True if point (x, y) (x and y both start at 0) is to be drawn black, False otherwise
"""
k = 960939379918958884971672962127852754715004339660129306651505519271702802395266424689642842174350718121267153782770623355993237280874144307891325963941337723487857735749823926629715517173716995165232890538221612403238855866184013235585136048828693337902491454229288667081096184496091705183454067827731551705405381627380967602565625016981482083418783163849115590225610003652351370343874461848378737238198224849863465033159410054974700593138339226497249461751545728366702369745461014655997933798537483143786841806593422227898388722980000748404719
return ((k + y)//17//2**(17*int(x) + int(y)%17))%2 > 0.5
```

```

You can test this function with the following code, which "draws" the result of Tupper's formula into a text file.

``````import codecs
import os
with codecs.open("tupper.txt", "w", "utf-8") as f:
values = [[tuppers_formula(x, y) for x in range(106)] for y in range(17)]
for row in values:
for value in row[::-1]: # x = 0 starts at the left so reverse the whole row
if value:
f.write("\u2588") # Write a block
else:
f.write(" ")
f.write(os.linesep)
```

```

The result is a file `tupper.txt`

with the content:

``````        █                   █                █ ██ █     █                █  █ █     █    █ ██ █      █   █
█                   █ █      █       █  █ █     █                █  █ █     █    █  █ █      █   █
██      █                  █  █      █    ██ █  █ █ █ █ █ ██ ████  ███ ███ █  █ █ █ █    █  █  █      █  █
█      █                  █  █  █ █ █       █ █  █  █  █    █ █ █ █ █ █ █ █  █ █ █ █    █ █   █      █  █
█      █                  █  █  █ █ █       █ █  █ █ █ █    █ █ █ ███ ███ █  █  █  █    █ █   █      █  █
█      █               █ █   █   █  █  ██        █     █                  █  █ █   █  █       █   ██  █ █
███   █ █               █ █   █  █   █ █  █       █     █                   █ █     █  █      █   █  █ █ █
█  █ ██ █   ██   ███ █   █      █   █        ███ ███                   █ ███ ███ █       █     █  █ █
███ █   █ █ █ █ █  █ █  █ █   █ ████ █  █                                                          █   █ █
█  █ █ █ █ █  █ █  █ █   █      █ █                                                          █    █ █
██    █ █ █ █ █  ██   ███ █   █ █ ██ █ ████                                                       ████ █ █
█     █                 █   █ █  █ █                                                          █      █ █
█      █                  █  █ █  █ █                                                          █     █  █
█       █                  █  █ █ █  █                                                         █      █  █
███     █                  █  █ █ █  █                                                                █  █
█                   █ █      █                                                               █   █
███                 █ ███  ███                                                               █ ███
```

```
+4

source

All Articles