Sujet : Re: "A diagram of C23 basic types"
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 18. Apr 2025, 12:49:56
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vtte95$2um97$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
User-Agent : Mozilla Thunderbird
On 18/04/2025 03:39, Lawrence D'Oliveiro wrote:
On Mon, 7 Apr 2025 22:46:49 +0100, bart wrote:
(In source code, it would also be useful to use 1e9 or 1e12,
unfortunately those normally yield floating point values.
Tried Python:
>>> type(1e9)
<class 'float'>
>>> round(1e9)
1000000000
>>> round(1e12)
1000000000000
However:
>>> round(1e24)
999999999999999983222784
So I tried:
>>> import decimal
>>> decimal.Decimal("1e24")
Decimal('1E+24')
>>> int(decimal.Decimal("1e24"))
1000000000000000000000000
which is more like it.
The idea behind writing 1e12 for example was for something that was compact, quick to type, and easy to grasp. This:
int(decimal.Decimal("1e24"))
seems to lack all of those. Besides:
import decimal
def fn(n):
for i in range(100_000_000):
a = int(decimal.Decimal(n))
print(a)
def fn2(n):
for i in range(100_000_000):
a = round(n)
print(a)
def fn3(n):
for i in range(100_000_000):
a = n
print(a)
fn("1e24") # 50 seconds (loop overheads excluded)
fn2(1e15) # 13 seconds
fn3(1e15) # 0.6 seconds
You don't want it to be much slower either: it should not affect performance. (Timings for CPython.)