Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7681253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:18:02+00:00 2026-05-31T18:18:02+00:00

Inspired by the encoding scheme of the answer to this question , I implemented

  • 0

Inspired by the “encoding scheme” of the answer to this question, I implemented my own encoding algorithm in Python.

Here is what it looks like:

import random
from math import pow
from string import ascii_letters, digits

# RFC 2396 unreserved URI characters
unreserved = '-_.!~*\'()'
characters = ascii_letters + digits + unreserved
size = len(characters)
seq = range(0,size)

# Seed random generator with same randomly generated number
random.seed(914576904)
random.shuffle(seq)

dictionary = dict(zip(seq, characters))
reverse_dictionary = dict((v,k) for k,v in dictionary.iteritems())

def encode(n):
    d = []
    n = n
    while n > 0:
        qr = divmod(n, size)
        n = qr[0]
        d.append(qr[1])
    chars = ''
    for i in d:
        chars += dictionary[i]
    return chars

def decode(str):
    d = []
    for c in str:
        d.append(reverse_dictionary[c])
    value = 0
    for i in range(0, len(d)):
        value += d[i] * pow(size, i)
    return value

The issue I’m running into is encoding and decoding very large integers. For example, this is how a large number is currently encoded and decoded:

s = encode(88291326719355847026813766449910520462)
# print s -> "3_r(AUqqMvPRkf~JXaWj8"
i = decode(s)
# print i -> "8.82913267194e+37"
# print long(i) -> "88291326719355843047833376688611262464"

The highest 16 places match up perfectly, but after those the number deviates from its original.

I assume this is a problem with the precision of extremely large integers when dividing in Python. Is there any way to circumvent this problem? Or is there another issue that I’m not aware of?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:18:03+00:00Added an answer on May 31, 2026 at 6:18 pm

    The problem lies within this line:

    value += d[i] * pow(size, i)
    

    It seems like you’re using math.pow here instead of the built-in pow method. It returns a floating point number, so you lose accuracy for your large numbers. You should use the built-in pow or the ** operator or, even better, keep the current power of the base in an integer variable:

    def decode(s):
        d = [reverse_dictionary[c] for c in s]
        result, power = 0, 1
        for x in d:
            result += x * power
            power *= size
        return result
    

    It gives me the following result now:

    print decode(encode(88291326719355847026813766449910520462))
    # => 88291326719355847026813766449910520462
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Inspired by this question and answer , how do I create a generic permutations
Inspired by this question. Why is there no list.clear() method in python? I've found
This question was inspired by this answer to another question, indicating that you can
Inspired by this question , I wanted to try my hand at the latest
Inspired by this question I began wondering why the following examples are all illegal
Inspired by this question on Meta , I wrote two queries on the Stack
Inspired by this question I decided to test the rank() function, trying to see
Inspired by this question . Short version: Why can't the compiler figure out the
Inspired by this question . I commonly see people referring to JavaScript as a
Inspired by this answer .. Can you guys point me to something similar--something that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.