1

I'm trying to get the result in hex format, but I get the error "TypeError: 'float' object cannot be interpreted as an integer!"

     39 d = chinese_remainder(a, n)
---> 40 number = hex(d)
     41 print(number)

Code:

import functools

# Euclidean extended algorithm
def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        d, x, y = egcd(b % a, a)
        return d, y - (b // a) * x, x

"""
    Functions whcih calculate the CRT (
    return x in ' x = a mod n'.
"""

def chinese_remainder(a, n):
    modulus = functools.reduce(lambda a, b: a * b, n)
    multipliers = []
    for N_i in n:
        N = modulus / N_i
        gcd, inverse, y = egcd(N, N_i)
        multipliers.append(inverse * N % modulus)

    result = 0
    for multi, a_i in zip(multipliers, a):
        result = (result + multi * a_i) % modulus
    return result

FN = 1184749
FM = 8118474
FL = 5386565
HN = 8686891
HM = 6036033
HK = 6029230

n = [FN, FM, FL]
a = [HN, HM, HK]

d = chinese_remainder(a, n)
number = hex(d)
print(number)

The result should be like this FAB15A7AE056200F9

But it gives me 3.3981196080447865e + 19

How to fix this so that the result is in hex format ???

JDop
  • 25
  • 1
  • 4
  • 2
    I am sure you wanted integer division: `N = modulus // N_i`. – DYZ Oct 18 '21 at 23:14
  • 1
    Please read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/, and start by trying to *read* and *understand* the error message. Where did the `float` value come from? Were you *expecting* it to be a `float` (rather than an `int`)? I assume not. Should *anything* in your program produce a `float` result? *Does* anything? – Karl Knechtel Oct 18 '21 at 23:18
  • The function `hex()` requires an input as an intiger. You are getting an error because you are passing a float, `d`, to this function. – 3ddavies Oct 18 '21 at 23:18
  • @3ddavies How do I get an integer in Python? It's just a script that calculates Chinese remainder theorems. And I really need to get the result in a hex format? – JDop Oct 18 '21 at 23:28
  • @JDop make sure you are using integer division, ie replace `N = modulus / N_i` with `N = modulus // N_i` – 3ddavies Oct 18 '21 at 23:31

1 Answers1

1

Normal division / operator returns float whereas you can use floor division // to get integers. As others suggested, you have to use floor division to variable N like this N = modulus//N_i

Python learner
  • 1,159
  • 1
  • 8
  • 20