casting constant value from float to unsigned short - compiler bugs?

Discussion in 'C Programming' started by, Dec 2, 2011.

  1. While I like nothing better that good barney about definitions, lets not
    lose sight of the fact that the C standard is not at all ambiguous about
    the conversion that sparked this thread. Talk of "readings" suggests
    that there might be some doubt, but there isn't -- C's conversion from
    floating to integer types "truncates towards zero".
    Ben Bacarisse, Jan 5, 2012
    1. Advertisements


    gwowen Guest

    Interesting that using the definition of "Integer Part" from MathWorld
    or PlanetMath invalidates this definition.

    Perhaps more interesting is MathWorld's comment on the fractional
    part: "Unfortunately, there is no universal agreement on the meaning
    of frac(x) for x<0 and there are two common definitions"
    gwowen, Jan 5, 2012
    1. Advertisements


    James Kuyper Guest

    That was my initial reaction, too, and I presume that provoking that
    reaction was the purpose of those lines. However, that doesn't quite
    make sense: anyone who decided to post a response complaining about the
    lack of a specific citation would immediately notice it, unless they
    made the mistake of top-posting.
    I'm basically in agreement with you on that, but playing the Devil's
    Advocate for a moment, I have to concede that his citation seems
    significantly more authoritative than Ben's.
    James Kuyper, Jan 5, 2012

    James Kuyper Guest

    On 01/05/2012 06:15 AM, Ben Bacarisse wrote:
    Michael has absolute certainty (justified or not) about the mathematical
    definition of "fractional part". Since the C standard provides no
    definition of it's own, if there were a single definition of the term
    that is universally used in the mathematical community, then that one is
    the one that should apply. If his definition is the correct one, the
    standard is self-contradictory: it specifies both that "the fractional
    part is discarded" and that "the value is truncated toward zero".
    James Kuyper, Jan 5, 2012
  5. I'd agree if the phrasing were "the fractional part is subtracted" but
    "to discard" is not a natural verb to use with the mathematical
    meaning[1] of fractional part. Given the mathematical meaning, the C
    standard would be saying that -3.2 is converted to an int by discarding
    0.8. What would that mean exactly?

    Other parts of the standard help. The decimal representation of
    floating point values, for example, are described as having a fractional

    [1] I'm just using this as a shorthand. I am not at all sure that there
    is one universal mathematical meaning of "fractional part".
    Ben Bacarisse, Jan 5, 2012
  6. The purpose is to allow you time to realize you asked
    a yes-no question; and I answered it. Then I graciously
    supplied you with citations.
    In case you are not interested.
    It is what I think, not feel.
    You asked a yes-no question.
    Did you realize that?
    I was and am.
    Oh ho. You are not interested in this; and are wasting my time,
    else you would present cogent arguments.
    Michael Press, Jan 6, 2012
  7. I never said the C standard is ambiguous.
    I note that it is at variance with the definition used in mathematics.
    Michael Press, Jan 6, 2012
  8. It as good as does.

    the fractional part is discarded
    (i.e., the value is truncated toward zero)
    You introduce correctness. For the record, I am not at all
    interested in correctness here. Good definitions are a
    result of much thought and work.

    Presumably truncating toward zero in the C standard
    is to reduce the number of cases where conversion leads to overflow.
    Michael Press, Jan 6, 2012
  9. Why?

    I was trying to have a constructive technical discussion. If you're
    not interested in that, I won't waste my time with you.
    In fact I am interested in this. You made a specific claim; I asked you
    to support it. Someone interested in actual discussion wouldn't have
    made such a big deal about the fact that my question was literally a
    yes/no question; it was clearly intended to solicit more useful

    My argument above was certainly intended to be "cogent". If you didn't
    find it so, feel free to elaborate.
    Keith Thompson, Jan 6, 2012

    James Kuyper Guest

    No, that's not in the Definitions section, nor are the words "fractional
    part" italicized: those are the two methods used by the standard to
    identify a definition. Therefore:

    If that were the case, then that paragraph would be a prime example of
    why it's generically a bad idea for the standard to specify something
    twice. In reality, your use of the definite article in your phrase "the
    mathematical definition" seems to be unjustified - alternative
    definitions are in use. One point of view looks at the value that would
    print out as "-23.45", and parses it as having a sign factor of -1, an
    integer part of 23, a decimal point, and a fractional part of 45/100.
    Under that interpretation, "discarding the fractional part" means
    converting "-12.34" into "-12.", and is indeed equivalent to truncation
    toward zero. Therefore, this isn't a double specification; the
    truncation clause clarifies which one of the multiple definitions
    currently in use for "fractional part" is being used in the discard clause.
    The only thing being defined here is the behavior, not the meaning of
    the terms used in that definition. You're right about good definitions,
    but one of requirements for a good definition, one of the reasons it
    requires careful thought, is that all of the terms used in that
    definition should be used correctly.
    I can't be sure what the reason was. Personally, I've never written code
    where truncation of negative numbers toward zero was the desired
    behavior. In most cases when I've converted a floating point number that
    might be negative to an integer, what I wanted was lrint(x) (but
    instead, I had to settle for (long)floor(x+0.5), because our contract
    with our client requires use of C90 code); the second most common thing
    I've wanted is (long)floor(x) - I can't remember ever wanting the result
    that I would have gotten for negative values of x from (long)x.

    The Rationale only says "There was strong agreement in the C89 Committee
    that floating values should truncate toward zero
    when converted to an integer type, the specification adopted in the
    Standard. Although K&R 35 permitted negative floating values to truncate
    away from zero, no C89 Committee member knew of an implementation that
    functioned in such a manner.2"
    Footnote 2 says "The Committee has since learned of one such
    James Kuyper, Jan 6, 2012
  11. Actually, it was to simplify programmers' lives by acknowledging nearly
    universal existing practice (and to agree with FORTRAN's requirements).
    lawrence.jones, Jan 6, 2012
  12. You offer a meta-argument about mathematics,
    rather than mathematical arguments. That
    is hand waving.

    For instance: the definition of prime
    number once included 1 as a prime.
    Eventually this definition made
    statements and proofs of theorems
    awkward. Changing the definition
    made theorem statements and proofs
    smoother and easier to follow.

    frac(x) = x - floor(x) is simpler
    to write than the alternative.
    The standard definition is periodic
    and that is a neat property to have.

    The definition of equidistributed sequence is easier.
    A sequence {a_n} is equidistributed
    if {frac(a_n)} is equidistributed in [0,1].

    The truncation toward zero definition does not
    make mathematics easier.

    argumentum ad verecundiam:
    Donald Knuth uses this definition.
    Michael Press, Jan 6, 2012
  13. Mathematica is not mathematics. C is not mathematics.
    Michael Press, Jan 6, 2012

    James Kuyper Guest

    This isn't a question about whether Mathematica "is mathematics"; no one
    claimed that it was. It isn't even a question of whether Mathematica's
    web site is authoritative on such issues - no one made that claim,
    either. The Mathematica web site claims that there are multiple
    definitions in use by mathematicians. The key question is whether or not
    that claim is correct, an issue that is completely independent of who
    made that claim. The Mathematica web site provides a citation in support
    of this claim. Any of the following would be relevant counter-arguments:

    1. Present evidence indicating that the cited text does not exist.
    2. Present evidence indicating that the cited text does not, in fact,
    say what Mathematica's web site says that it says.
    3. Argue that the authors of the cited text cited are insufficiently
    influential for their use of a different definition to support a claim
    that it is "one common definition". This is necessarily a judgement
    call, but it can at least reasonably be argued about.

    Personally, I'd consider 3 to be the most reasonable target to attack -
    I certainly have no idea who those authors were, which places a (very
    high) upper limit on how influential they could be.

    But you didn't choose any of those approaches. Instead you argue
    irrelevancies, like "Mathematica is not mathematics".
    James Kuyper, Jan 6, 2012

    James Kuyper Guest

    Facts like that would be relevant, if the question Keith were discussing
    were "what should the mathematical definition of 'fractional part' be?".
    The question he was actually discussing was "Is there a definition for
    'fractional part' that is so widely accepted by mathematicians, that it
    can reasonably be referred to as 'the' mathematical definition?".

    The benefits and disadvantages of various possible definitions are very
    relevant to the first question, and completely irrelevant to the second
    one. Identifying people who use various definitions for 'fractional
    part', and assessing their degree of influence in the mathematical
    community, should be irrelevant to the first question, but is very
    relevant to the second one.

    The wording used in the standard is quite reasonable if the answer to
    the second question is "No", while the answer to the first question is
    quite irrelevant. The behavior defined by the standard was chosen to
    match common existing usage, not because of any of the issues you raise
    above. The only legitimate issue is about how to correctly describe that
    James Kuyper, Jan 6, 2012

    Tim Rentsch Guest

    I disagree with this thesis. The Standard is not a math book;
    what it is is a standards document. Any term not defined in the
    Standard itself, nor appearing in one of the listed references,
    should be understood as used in "the general population", not by
    defaulting to any more restricted subgroup, including mathematics.
    Tim Rentsch, Jan 26, 2012

    Tim Rentsch Guest

    Just an observation here -- you are also.
    Tim Rentsch, Jan 26, 2012
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.