Print all decimals

-1

You must write a program or function that will output all decimals with the specified precision if run forever. Example:

Input:

2

Output:

0.01, -0.01, 0.02, -0.02, 0.03, -0.03...

The input is the number of decimal places to print. The output is all the numbers with the specified decimal precision (if run forever). It will not output 0.

This challenge has much in common with Print all integers, and the rules are the same:

  • The output must be in decimal, unless your language does not support decimal integer (in that case use the natural representation of integers your language uses).
  • Your program has to work up to the numbers with the biggest magnitude of the standard integer type of your language.
  • Each integer must be separated from the next using any separator (a space, a comma, a linebreak, etc.) that is not a digit nor the negative sign of your language.
  • The separator must not change at any point.
  • The separator can consist of multiple characters, as long as none of them is a digit nor the negative sign (e.g. , is as valid as just ,). Any supported integer must eventually be printed after a finite amount of time.

But one more additional rule:

  • Your submission must support the highest input value / precision possible for floating point arithmetic. You don't have to handle input value beyond that.

Additional test cases:

4

-0.0001 0.0001 -0.0002 0.0002 -0.0003 0.0003...

Note that there can be multiple valid outputs,

0.0001 0.0002 0.0003 -0.0001 -0.0002 -0.0003 0.0004 0.0005 0.0006 -0.0004 -0.0005 -0.0006...

is valid for that last example.

The ellipsis (...) means more input will follow.

programmer5000

Posted 2017-04-27T23:26:45.010

Reputation: 7 828

Question was closed 2017-04-27T23:47:52.720

1In your output it is not obvious when 0 would be printed – Digital Trauma – 2017-04-27T23:29:48.903

"unless your language does not support decimal integer (in that case use the natural representation of integers your language uses)"..."Any supported integer must eventually be printed after a finite amount of time." - copy-paste? – Jonathan Allan – 2017-04-27T23:32:27.447

3It looks like you can just print all the integers, dividing each by 10^n. Is this sufficiently different a challenge? – xnor – 2017-04-27T23:36:28.760

I think this is close enough to be considered a dup. In languages that support non-integers, I think the answers will mostly be trivially similar. I won't cast my [tag:code-golf] dup-hammer vote yet though - lets see what others think... – Digital Trauma – 2017-04-27T23:36:52.827

I think you need to define "precision" 0.0001 having precision 4 suggests to me that the meaning is number of decimal places (in this case, shouldn't 0.0000 be printed? What is different between one of four and zero of four?), What about 1.0001? – Jonathan Allan – 2017-04-27T23:44:37.900

Nitpick: This doesn't print all decimals. It doesn't even print all rationals. – Dennis – 2017-04-28T00:12:51.530

Answers

1

R, 56 Bytes

i=scan();j=1;while(TRUE){l=j/(10^i);j=j+1;cat(l,-l," ")}

Neil

Posted 2017-04-27T23:26:45.010

Reputation: 2 417