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

B

Ben Bacarisse

Michael Press said:
What is your point here?
What definition of fractional part does your reading imply?

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".
 
G

gwowen

Yes. Why? Do you have a source that states otherwise?

Knuth, ACP, 1.2.4.

Oxford Dictionary of Mathematics
<http://books.google.com/books?id=UTCenrlmVW4C&pg=PT192&lpg=PT192&dq=D...>

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"

http://mathworld.wolfram.com/IntegerPart.html
http://planetmath.org/encyclopedia/IntegerPart.html
http://mathworld.wolfram.com/FractionalPart.html
 
J

James Kuyper

Michael Press said:
[snip 116 blank lines]
What was the purpose of those blank lines?

I will wait until Keith asks.

Ok, what was the purpose of those blank lines? And why would you feel
the need to wait until I ask about it?

When I read your response, I didn't even realize that those blank lines
were there. I went to the next article thinking that you had said that
you have a source but weren't going to share it (and was about to remark
on that.

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.
My working hypothesis is that you were being deliberately difficult.
Agreed.


Ok. My point is that "mathematics" is a very large field, and I'm
skeptical that there's a single definition for a term like "fractional
part" that all mathematicians would accept.

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.
 
J

James Kuyper

On 01/05/2012 06:15 AM, Ben Bacarisse wrote:
....
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".

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".
 
B

Ben Bacarisse

James Kuyper said:
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".

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
part.

[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".
 
M

Michael Press

Keith Thompson said:
Michael Press said:
On 01/04/2012 06:14 PM, Michael Press wrote:
Hence frac(x) >= 0.

Can you cite a source that says that that's what "fractional part"
means in mathematics?

Yes. Why? Do you have a source that states otherwise?

At the time Keith posted, Ben Pfaff had already given a URL for a
Mathematica web site indicating that "Spanier and Oldham (1987) use the
same definition as Mathematica", which is (probably not coincidentally)
the same as C's definition.

...
[snip 116 blank lines]
What was the purpose of those blank lines?

I will wait until Keith asks.

Ok, what was the purpose of those blank lines?

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.
And why would you feel
the need to wait until I ask about it?

In case you are not interested.
It is what I think, not feel.
When I read your response, I didn't even realize that those blank lines
were there. I went to the next article thinking that you had said that
you have a source but weren't going to share it (and was about to remark
on that.

You asked a yes-no question.
Did you realize that?
My working hypothesis is that you were being deliberately difficult.

I was and am.
Ok. My point is that "mathematics" is a very large field, and I'm
skeptical that there's a single definition for a term like "fractional
part" that all mathematicians would accept.

Oh ho. You are not interested in this; and are wasting my time,
else you would present cogent arguments.
 
M

Michael Press

Ben Bacarisse said:
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".

I never said the C standard is ambiguous.
I note that it is at variance with the definition used in mathematics.
 
M

Michael Press

James Kuyper said:
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,

It as good as does.

the fractional part is discarded
(i.e., the value is truncated toward zero)
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".

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.
 
K

Keith Thompson

Michael Press said:
I was and am.

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.
Oh ho. You are not interested in this; and are wasting my time,
else you would present cogent arguments.

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
information.

My argument above was certainly intended to be "cogent". If you didn't
find it so, feel free to elaborate.
 
J

James Kuyper

It as good as does.

the fractional part is discarded
(i.e., the value is truncated toward zero)

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.
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.

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.
Presumably truncating toward zero in the C standard
is to reduce the number of cases where conversion leads to overflow.

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
implementation."
 
L

lawrence.jones

Michael Press said:
Presumably truncating toward zero in the C standard
is to reduce the number of cases where conversion leads to overflow.

Actually, it was to simplify programmers' lives by acknowledging nearly
universal existing practice (and to agree with FORTRAN's requirements).
 
M

Michael Press

Keith Thompson said:
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
information.

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

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.
 
M

Michael Press

Michael Press said:
Vincenzo Mercuri said:
p1:
When a finite value of real floating type is converted to an
integer type other than _Bool, the fractional part is discarded
(i.e., the value is truncated toward zero). If the value of the
integral part cannot be represented by the integer type,
the behavior is undefined.[^50]

Note that this wording is at variance with standard mathematcal definitions.

If http://mathworld.wolfram.com/FractionalPart.html is to be
believed, there is no single standard mathematical definition of
"fractional part", but the one used in the C standard matches one
common definition.

Mathematica is not mathematics. C is not mathematics.
 
J

James Kuyper

Mathematica is not mathematics. C is not mathematics.

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".
 
J

James Kuyper

Keith Thompson said:
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
information.

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

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.

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
behavior.
 
T

Tim Rentsch

James Kuyper said:
On 01/05/2012 06:15 AM, Ben Bacarisse wrote:
...
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".

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. [snip]

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.
 
T

Tim Rentsch

Michael Press said:
Keith Thompson said:
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
information.

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

You offer a meta-argument about mathematics,
rather than mathematical arguments. That
is hand waving. [snip]

Just an observation here -- you are also.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top