3

I am trying to run a test if the number ends in .0

I am running a program with numbers orders of magnitude apart so I can't estimate to a certain amount of digits. using % doesn't work either because then certain numbers are excluded. All the numbers in this program are floating point numbers so I need a way to check if it ends with .0, not with .00000000000001232 or something it has to end exactly in .0

The problem with the round function is that I am dealing with numbers of several orders of magnitude. I need something that checks if it has only 1 decimal after the . or something that checks if the that decimal is a 0.

code:

from myro import *
from math import *


def main():

    z = 3
    a = 2
    b = 2
    x = 3
    y = 2 #starts at y = 3

    lim = 25

    c = (a**x + b**y)**(1.0/z)

    resultnum = 0    

    while z <= lim:

        while a <= lim:

            while b <= lim:

                while x <= lim:

                    while y <= lim:

                        y = y + 1
                        c = (a**x + b**y)**(1.0/z)

                        if float(int(c) + 1) != round(c, 6):
                            pass
                        else:
                            print str(a) + "^" + str(x) + " + " + str(b) + "^" + str(y) + " = " + str(int(c)+1) + "^" + str(z)
                            resultnum = resultnum + 1
                            print c

                    y = 3
                    x = x + 1

                x = 3
                b = b + 1

            b = 3
            a = a + 1

        a = 3
        z = z + 1
        print z

    print "code cycle complete"
    print str(resultnum) + " results"


main()
karthikr
  • 97,368
  • 26
  • 197
  • 188
Felis Vulpes
  • 33
  • 2
  • 7

4 Answers4

8
>>> n1 = 2.0
>>> int(n1) == n1 and isinstance(n1, float)
True
>>> n1 = 2
>>> int(n1) == n1 and isinstance(n1, float)
False
>>> n1 = 2.01
>>> int(n1) == n1 and isinstance(n1, float)
False
>>> n1 = 1E1         #works for scientific notation as well
>>> int(n1) == n1 and isinstance(n1, float)
True
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
4

Python does this already. Going with what Python gives as a string might be what you want:

In [577]: def dot_zero(number):
   .....:     return str(number).endswith('.0')
   .....: 

In [578]: dot_zero(2.0)
Out[578]: True

In [579]: dot_zero(2)
Out[579]: False

In [580]: dot_zero(2.01)
Out[580]: False

EDIT

As pointed out by @jamylak this does not work for large numbers since the scientific notation used by str. Keeping the basic idea of conversion into a string, but also catering for large numbers, we end up with more verbose and admittedly rather ugly solution:

def dot_zero_string(number):
    # tested and works with Python 3.3
    split = str(number).split('e')
    return len(split) == 2 or split[0].endswith('.0')

This is the solution in the answer from @AshwiniChaudhary

def dot_zero_inst(number):
    return int(number) == number and isinstance(number, float)

Comparing different cases gives the same result:

numbers = [1, 1.0, 1000, 1000.0, 3e38, 1.5555555555555555555555e12, 
           1.5555555555555555555555e17, 0, 0.0]
numbers = numbers + [-number for number in numbers]
for number in numbers:
    assert dot_zero_inst(number) == dot_zero_string(number)
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
0

Just to show another method, you can always split by the '.':

>>> num = 12.023
>>> str(num).split('.')[1] == '0'
False
>>> num = 12.0
>>> str(num).split('.')[1] == '0'
True

Note that this works because you said that all were floating points. This will provide an error num is an int

Ryan Saxe
  • 17,123
  • 23
  • 80
  • 128
0
x = 26.5
b % math.floor(b) == 0
>>> False

x = 26.0
b % math.floor(b) == 0
>>> True

should also do it.

Dome271
  • 81
  • 2
  • 11