Richard Heathfield <(E-Mail Removed)> writes:

> Eric Sosman wrote:

>> On 6/1/2010 4:35 AM, Richard Heathfield wrote:

>>> In<hmWMn.34334$8g7.14401@hurricane>, bart.c wrote:

>>>

>>> <snip>

>>>>

>>>> You might just try testing every possible value:

>>>>
[snip]

>>>

>>> If there is genuine cause for concern, unsigned short is sufficiently

>>> large to make your program rather tedious to run. It would make more

>>> sense to binary-search. Start at USHRT_MAX, and play the max/min

>>> game.

>>

>> Not sure how you'd apply binary search to this problem, since

>> there's no obvious "order:" each test tells you that a given value

>> does or does not garble when converted to double and back, and gives

>> no obvious clue about what happens to larger and smaller values.

>> Did you have some other order relation in mind?

>

> Oh, it was just a faulty (albeit probably fairly accurate!) assumption -

> that double will be able to represent integers precisely, up to a

> certain point, but not (reliably) beyond that point. If there is such a

> point for unsigned short ints, it would be nice to know what it is.
But since the non-representable values aren't consecutive, a simple

binary search might not find the smallest one.

It's probably safe to assume that if N-1 and N are both exactly

representable, then all values from 0 to N are exactly representable.

Given that assumption, you could do a binary search to find the

smallest non-representable value.

> (Having said that, I suspect that the range of contiguous integer values

> starting from 0 and exactly representable by double is likely to exceed

> USHRT_MAX on any "normal" implementation, so there is probably no such

> point.)
Well, I've used a system (Cray T90) where both short and double

were 64 bits. But short may have had padding bits; I never had a

chance to check.

--

Keith Thompson (The_Other_Keith)

(E-Mail Removed) <http://www.ghoti.net/~kst>

Nokia

"We must do something. This is something. Therefore, we must do this."

-- Antony Jay and Jonathan Lynn, "Yes Minister"