Quantcast
Viewing all articles
Browse latest Browse all 13

Answer by Daniel Stutzbach for Checking if float is equivalent to an integer value in python

Under the hood, Python's float type is a C double.

The most robust way would be to get the nearest integer to num, then test if that integers satisfies the property you're after:

import mathdef is_triangular1(x):    num = (1/2) * (math.sqrt(8*x+1)-1 )    inum = int(round(num))    return inum*(inum+1) == 2*x  # This line uses only integer arithmetic

Viewing all articles
Browse latest Browse all 13

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>