Article View: comp.lang.c
Article #892Re: .5 against 0.5f
From: James Kuyper
Date: Tue, 27 Oct 2026 21:43
Date: Tue, 27 Oct 2026 21:43
63 lines
4384 bytes
4384 bytes
On 2026-10-27 13:45, Steven G. Kargl wrote: > On Tue, 27 Oct 2026 11:53:55 +0100, fir wrote: > >> if ai generates me some functions it puts everywhere this f at the and >> yet it add zeros so instead of .1 i got 0.1f >> >> and i obviously dont like it >> >> i write hvever some codes when i need speed so the question is may it >> give some difference? >> >> i mean .1 maybe can be treated as doube constants so mabe occupies some >> more memory (?) or uses some casting in coplutations (?) >> >> >> i doubt it probably it makes difference but im strictly not sure > > The leading zero in 0.1 or 0.1f is irrelevant, but (to me) makes > code easier to read. The difference between 0.1 and 0.1f with > their binary representations is a another matter. The number 0.1 cannot be represented exactly in binary. The C constant "0.1" implicitly has a double value. It has "either the nearest representable value" to exactly 0.1, "or the larger or smaller representable value immediately adjacent to the nearest representable value, chosen in an implementation-defined manner." (6.5.4.3p4). Since it's double precision constant, "representable" means "representable as a double precision number". 0.1f, on the other hand, is explicitly a float value. It follows the same rules, but with "representable" meaning "can be represented as a float". 0.1 and 0.1f will therefore not be the same. If you convert 0.1f to double precision, it will convert the floating point value actually stored to double precision, not the real value it represents. "if the value being converted can be represented exactly in the new type, it is unchanged." (6.2.4p4) Any float value is normally representable exactly as a double, simply by setting all of the bits that are not stored in float format to 0 in double format. Those bits will therefore not have the same value they would have in "0.1". If you convert 0.1 to float, y, "... the value being converted is in the range of values that can be represented but cannot be represented exactly," so "the result is either the nearest higher or nearest lower representable value, chosen in an implementation-defined manner." (6.3.2.5p2). Note that "nearest" is, in this case, being determined relative to the floating point value represented by "0.1", not the mathematical value of 1/10. Also, the "implementation-defined manner" need not be the same as the one used for determining the value of floating point constants. So, 0.1 and 0.1f have different values, and might be even more different after either one is converted to the other's type for comparison. The difference is only in the last representable bits, which won't matter for most practical purposes, but will matter if you're comparing for exact equality. You should use 0.1 when the value will be stored in a double or would otherwise immediately get converted to double when used (which is often the case). You should use 0.1f to initialize a float variable or to pass an argument to a function whose prototype specifies that the corresponding parameter has the type float.
Technical Details
Message-ID:
<11br5td$1to7l$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11bpvs7$1e6kc$2@dont-email.me> <11bqnvh$1np7s$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: James Kuyper <jameskuyper@alumni.caltech.edu> Newsgroups: comp.lang.c Subject: Re: .5 against 0.5f Date: Tue, 27 Oct 2026 17:43:08 -0400 Organization: A noiseless patient Spider Lines: 63 Message-ID: <11br5td$1to7l$1@dont-email.me> References: <11bpvs7$1e6kc$2@dont-email.me> <11bqnvh$1np7s$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Injection-Date: Tue, 27 Oct 2026 21:43:10 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2023669"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/U8NmlqFDZ2Hfh9z3JEnWTnkZZo0kG6PM="; posting-host="12d93631e700fb3ab499ef75e583e1ff" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:zn5NhLLHwBxVr8TKXltm6w3FLyE= sha256:5NvU2vqSAhL+A/Hn/0v7HnjLyzyuliU+98BZmRWwdAI= sha1:7EpEvClhlfmVGVDQxBUvD++Dq4k= sha256:FPNs0oJVhBfrvOutOqtpEhEioBENNA0WmKq+OB8wXtM= Content-Language: en-US In-Reply-To: <11bqnvh$1np7s$1@dont-email.me>