• Re: .5 against 0.5f

    From Kaz Kylheku@3:633/10 to All on Tuesday, November 03, 2026 18:25:53
    On 2026-11-03, Dan Cross <cross@spitfire.i.gajendra.net> wrote:
    In article <11c8dls$2chr0$1@dont-email.me>,
    James Kuyper <jameskuyper@alumni.caltech.edu> wrote:
    On 2026-10-29 17:55, Janis Papanagnou wrote:
    On 2026-10-29 20:49, Dan Cross wrote:
    [...]

    But again, the whole premise (that a programmer familiar with C
    would ever think that `return` is a function) is silly.

    Agreed. The misconception is far more common among those
    who aren't very familiar with C.

    It is not common at all.

    You have to get to a certain advanced level of education in computer
    science before you start believing that returning is another function
    calls.

    But it's there. The returning control flow can be modeled by
    continuations.

    We can eliminate returns from a program by passing a context
    argument to each function: the return continuation. The places
    where the function returns become tail calls to the continuation.

    The transformation of a program to continuation passing style ("CPS transformation") can be done by a compiler, allowing a program in a
    language in which ordinary function returning appears to work under the
    hood by returns achieved by a function call.

    How it works is that all the code that would take place in the
    caller after a function returns is bundled into a lambda body,
    and the lambda is passed as the continuation argument.

    (This is now actually very familiar to youngsters who work in web front
    end and know about "async" functions. In that paradigm, a kind of
    continuation system appears whereby the computation we'd like to do from
    now on, but only after some event is received, is farmed out to a
    lambda.)

    Famously, (to tie this back to C somehow), Chicken Scheme implements continuation over the native stack in a particular way. Chicken Scheme
    code is translated to C code, and that C code never returns from its
    functions. It always passes a lambda to the callee. Lambdas capture
    their context on the stack. Because nothing ever returns, pointers and
    other objects on the stack nicely persist: local variables don't die
    after a function returns because it's compiled to a C function that
    returns by calling a continuation. When the stack hits a certain limit,
    it is rewound back to the top. At that time, all the objects in the
    stack that are still live are identified and moved to the heap. I think,
    even character strings in their entirety (the actual array) are
    bump-allocated on the stack, and so have to be memmoved to the heap when
    the stack rewinds, so the stack acts as a nursery for new objects.


    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @Kazinator@mstdn.ca

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Tim Rentsch@3:633/10 to All on Thursday, November 05, 2026 06:31:59
    antispam@fricas.org (Waldek Hebisch) writes:

    Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:

    David Brown <david.brown@hesbynett.no> writes:

    On 28/10/2026 21:25, Michael S wrote:

    [...]

    I'd still likely prefer to write it as (float)0.1 instead of 0.1f.
    It somewhat depends on my mood in specific day, but likelihood is
    certainly not in favor of 0.1f.

    That seems a bit excessive to me, but okay. The principle is the
    same, making it clear to the reader and the compiler that no
    double-precision arithmetic is involved.

    Except that, at least in the abstract machine, `(float)0.1` produces
    a value of type double and converts it to float. It's likely that
    a compiler would generate the same code as for `0.1f`, but I'd want
    to confirm that if it mattered. (Actually, I'd just write `0.1f`.)

    Maybe the code will be the same. But in general going via doubles
    produces less accurate result, [...]

    What makes you think using a double will produce a less accurate
    result?

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Waldek Hebisch@3:633/10 to All on Thursday, November 05, 2026 22:07:10
    Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
    antispam@fricas.org (Waldek Hebisch) writes:

    Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:

    David Brown <david.brown@hesbynett.no> writes:

    On 28/10/2026 21:25, Michael S wrote:

    [...]

    I'd still likely prefer to write it as (float)0.1 instead of 0.1f.
    It somewhat depends on my mood in specific day, but likelihood is
    certainly not in favor of 0.1f.

    That seems a bit excessive to me, but okay. The principle is the
    same, making it clear to the reader and the compiler that no
    double-precision arithmetic is involved.

    Except that, at least in the abstract machine, `(float)0.1` produces
    a value of type double and converts it to float. It's likely that
    a compiler would generate the same code as for `0.1f`, but I'd want
    to confirm that if it mattered. (Actually, I'd just write `0.1f`.)

    Maybe the code will be the same. But in general going via doubles
    produces less accurate result, [...]

    What makes you think using a double will produce a less accurate
    result?

    Going via doubles number in the C source will be rounded twice,
    first time to a double, then the result of first runding will
    be rounded second time. Each rounding step introduces some
    error. Resonable heuristic says that errors may be close to
    maximal possible and may add. So error may be bigger than
    from single rounding.

    If you do not want to belive in the heuristic above, consider
    IEEE arthmetic and two consequtive single precisin numbers
    a and b. Next, take double c in the middle of the interval
    between a nad b. According to IEEE "round to even" rule this
    double will be rounded to one of the singles above, say a. Take
    source number d that rounds to c, but is on the side corresponding
    to b. Going via double will produce a as the result. Directly
    rounding to single will produce b. It is clear that b is
    closer to d than a, so double rounding in this case indeed
    produces more error, that is less accuracy.

    --
    Waldek Hebisch

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Dan Cross@3:633/10 to All on Friday, November 06, 2026 12:03:34
    In article <20261103101134.247@kylheku.com>,
    Kaz Kylheku <046-301-5902@kylheku.com> wrote:
    On 2026-11-03, Dan Cross <cross@spitfire.i.gajendra.net> wrote:
    In article <11c8dls$2chr0$1@dont-email.me>,
    James Kuyper <jameskuyper@alumni.caltech.edu> wrote:
    On 2026-10-29 17:55, Janis Papanagnou wrote:
    On 2026-10-29 20:49, Dan Cross wrote:
    [...]

    But again, the whole premise (that a programmer familiar with C
    would ever think that `return` is a function) is silly.

    Agreed. The misconception is far more common among those
    who aren't very familiar with C.

    It is not common at all.

    You have to get to a certain advanced level of education in computer
    science before you start believing that returning is another function
    calls.

    But it's there. The returning control flow can be modeled by
    continuations.

    Sure. But you don't seriously believe that's what Kuyper was
    referring to, do you?

    - Dan C.


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Chris M. Thomasson@3:633/10 to All on Friday, November 06, 2026 19:23:33
    On 10/29/2026 3:06 AM, David Brown wrote:
    On 28/10/2026 22:34, Chris M. Thomasson wrote:
    On 10/28/2026 12:39 AM, David Brown wrote:
    On 28/10/2026 01:49, Lawrence D?Oliveiro wrote:
    On Tue, 27 Oct 2026 18:06:25 -0400, James Kuyper wrote:

    However, if you need double precision, you shouldn't be using the
    "f" suffix.

    Is there any situation where you *should* use the ?f? suffix?

    I am thinking: no.

    Try thinking harder.

    I use the "f" suffix all the time in my code, because I do not want
    double precision.ÿ In many situations, single precision arithmetic is
    at least twice as fast as double precision - in some situations it
    can be two orders of magnitude faster.



    Same here. I am mostly creating graphics related things. double
    precision in a GPU might not be there. Now, if I am doing something
    that needs higher precision, tends to be with deep zooms or
    granularity in a vector field, then double is okay. But I need to
    beware that the shaders might not support double.

    You can also see it in SIMD.ÿ A 128-bit SIMD register might be useable
    for 4 single-precision values and parallel operations, while only 2 can
    be handled for double-precision.ÿ Obviously details will vary by SIMD implementation, but singles are going to be at least twice as fast in general.


    Indeed....

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)