Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Floating over the integers

As you may know, the typical binary floating point numbers in a computer language are quite different than the typical integer numbers in a computer language. Floating point numbers can represent a much larger range, and can represent fractional amounts, however there is a trade-off that is frequently elided.

The distance between integers is constant, and in fact the value of that constant is one. The distance between floating point numbers, however, is variable, and increases the further one travels down the number line, away from the origin at 0. At some point, that distance is so large that the floating point numbers start skipping over integers, and many integers that are technically less than the maximum value of the floating point numbers, cannot actually be represented by floating point numbers.

The challenge:

Write a program that when given the number n, will print out the first n positive integers that cannot be represented by a floating point number in your language of choice.

  • "First n positive integers" means, lowest in value, closest to the origin, and distinct from each other.

  • Infinite precision floating point numbers are not allowed.

  • It doesn't matter whether you choose 32 bit floats, 64 bits floats, or some other type of float, as long as it is a non-infinite precision floating point number in your language of choice, on your machine of choice.

  • Order of numbers does not matter

  • This is Code Gofl. Smallest number of characters wins.

  • Good luck and have fun

Example:

In Rust, using 32 bit floating point builtin type 'f32':

n=4, result = [16777217,16777219,16777221,16777223]

(updated - 1 order doesnt matter - 2 Golf->Gofl)

Answer*

Draft saved
Draft discarded
Cancel
6
  • \$\begingroup\$ So sad SpiderMonkey do not support bigint yet. Otherwise we can save many bytes by switching to SpiderMonkey and use print. \$\endgroup\$ Commented Feb 26, 2019 at 9:06
  • \$\begingroup\$ Number(++x)!=x saves 5 bytes. \$\endgroup\$ Commented Feb 26, 2019 at 9:30
  • \$\begingroup\$ @Neil I remembered that one cannot mix number with bigint. But I didn't know they can check equality without explicit type convertion.+(++x+'') would be shorter than Number(++x). \$\endgroup\$ Commented Feb 26, 2019 at 9:42
  • \$\begingroup\$ Would something like this work? \$\endgroup\$ Commented Feb 26, 2019 at 16:31
  • \$\begingroup\$ @Oliver It should work for small n. Try to pass n = 1000 and initial x as 2**54, you will find out the difference. \$\endgroup\$ Commented Feb 27, 2019 at 2:31

AltStyle によって変換されたページ (->オリジナル) /