python 3.x - Imprecise floats in Tupper's self-referential formula -


i trying make python program plots tupper's self-referential formula , have run problems.

first program couldn’t handle big floats had handle decided use bigfloats sort out problems. worked, sort of. problem have 540 digits long number needs multiplied bigfloat , when rounds number making inexact cause problems later. have tried raise precision 1000 , still keeps rounding variable.

the thing of numbers can processed without bigfloat exact value numbers can neither processed nor bigfloat right now.

here 1 of calculations goes wrong (this 1 able processed without bigfloat):

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

that code should return 1 instead returns 0.

is there way make bigfloat more accurate can use in program?

you don't need floating point math tupper's formula. trick 2-x same 1/2x have work integers because not need calculate floating point number 2-x , multiply integer instead calculate integer 2x , divide other integer integer. applied on tupper's formula 2-17*int(x) - int(y)%17 part becomes 1/217*int(x) + int(y)%17.

see following version of tupper's function operates in integer domain (in case not know ** is, python's power operator):

def tuppers_formula(x, y):     """return true if point (x, y) (x , y both start @ 0) drawn black, false otherwise     """     k = 960939379918958884971672962127852754715004339660129306651505519271702802395266424689642842174350718121267153782770623355993237280874144307891325963941337723487857735749823926629715517173716995165232890538221612403238855866184013235585136048828693337902491454229288667081096184496091705183454067827731551705405381627380967602565625016981482083418783163849115590225610003652351370343874461848378737238198224849863465033159410054974700593138339226497249461751545728366702369745461014655997933798537483143786841806593422227898388722980000748404719     return ((k + y)//17//2**(17*int(x) + int(y)%17))%2 > 0.5 

you can test function following code "draws" result of tupper's formula text file.

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

the result file tupper.txt content:

        █                   █                █ ██ █     █                █  █ █     █    █ ██ █      █   █         █                   █ █      █       █  █ █     █                █  █ █     █    █  █ █      █   █ ██      █                  █  █      █    ██ █  █ █ █ █ █ ██ ████  ███ ███ █  █ █ █ █    █  █  █      █  █  █      █                  █  █  █ █ █       █ █  █  █  █    █ █ █ █ █ █ █ █  █ █ █ █    █ █   █      █  █  █      █                  █  █  █ █ █       █ █  █ █ █ █    █ █ █ ███ ███ █  █  █  █    █ █   █      █  █  █      █               █ █   █   █  █  ██        █     █                  █  █ █   █  █       █   ██  █ █ ███   █ █               █ █   █  █   █ █  █       █     █                   █ █     █  █      █   █  █ █ █      █  █ ██ █   ██   ███ █   █      █   █        ███ ███                   █ ███ ███ █       █     █  █ █ ███ █   █ █ █ █ █  █ █  █ █   █ ████ █  █                                                          █   █ █      █  █ █ █ █ █  █ █  █ █   █      █ █                                                          █    █ █ ██    █ █ █ █ █  ██   ███ █   █ █ ██ █ ████                                                       ████ █ █   █     █                 █   █ █  █ █                                                          █      █ █  █      █                  █  █ █  █ █                                                          █     █  █ █       █                  █  █ █ █  █                                                         █      █  █ ███     █                  █  █ █ █  █                                                                █  █         █                   █ █      █                                                               █   █         ███                 █ ███  ███                                                               █ ███ 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -