# Machine Epsilon

Machine epsilon is an important floating point number to know when doing numerical calculations. One way to understand it is when this relation

1 + machine_epsilon > 1


does not hold. One (iterative) way to extract it is executing a small program like the one above:

10 EPS=1
20 EPS=EPS/2
30 EPS1 = 1+EPS
40 IF EPS1 > 1 GOTO 20
50 PRINT EPS


But there may be more ways. Write the shortest code to extract the machine epsilon of your computer. NOTE: epsilon should be a non-zero value :)

• no standard library (msdn.microsoft.com/en-us/library/system.single.epsilon.aspx ) nor literally typing out the memory representation of the epsilon? It's better to remind about it – Ming-Tang Jul 19 '11 at 21:18
• shinkirou: Read the page you linked: »Note: The value of the Epsilon property is not equivalent to machine epsilon, which represents the upper bound of the relative error due to rounding in floating-point arithmetic.« – Joey Jul 20 '11 at 8:08

## PowerShell, 5

Since .NET has certain guarantees for double, this number will not change, regardless of the implementation of the CLR or the platform it runs on. Therefore, the following suffices:

1/8pb


If you desperately need an iteration that computes the value in the way you've given in the task description, then that'd be 25 characters:

for($e=1;1+$e-1){$e/=2}$e

• what is 8pb? so you mean that there is no way to calculate value doing some kind of iteration? – leonardo Jul 19 '11 at 17:59
• 8PB is 9007199254740992 (i.e. the number of bytes in eight pebibytes). Of course there is, but since it's always the same value there isn't much point in using longer code to achieve the same result, I guess. – Joey Jul 19 '11 at 18:14
• well, i am not so desperate :) thanks for your answer! – leonardo Jul 19 '11 at 18:53

## ><> - 18 16 characters

1>2,:1v
n^?)1+<;


Usage and output using the python interpreter:

$fish scripts/epsilon.fish 1.1102230246251565e-16  Edit Fixed code to return correct value (1.1e-16) • impressed! nice compact code. – leonardo Jul 19 '11 at 17:31 • I don't think this is different enough to warrant its own answer (it's the same thing just put a different way). 14 bytes with this: line 1: 12,:1+1=0$. line 2: ;n – cole Nov 6 '15 at 5:16

## JavaScript, 28 characters

for(y=1;y+1-1;y/=2);alert(y)


I could replace the y+1-1 with y+1>1, but I like for aesthetics of the former.

Brainfuck, 16

Brainfuck doesn't support floating-point arithmetic, so epsilon is 0.

-[>-<+++++]>---.


According to this, this is the smallest such program. Program assumes that cells are unsigned bytes mod 256.

• The Wikipedia page gives the following example: »A trivial example is the machine epsilon for integer arithmetic on processors without floating point formats; it is 1, because 1+1=2 is the smallest integer greater than 1.« – Joey Jul 19 '11 at 19:53
• Yeah... I'd tend to agree, but the problem asks for 1+eps <= 1. – boothby Jul 19 '11 at 19:55
• Hm, indeed. The answer should therefore be half the 2.2e-16 on WP for doubles (so the Fish one is probably wrong here). Ah well, fixing my answer yet again :) – Joey Jul 19 '11 at 20:07
• @Joey, my answer could be wrong here, but both the python answer and the python example on wikipedia both state 2.2e-16. The ><> script actually gives 1.1e-16, but it doubles it before printing because I figured it has to be the same as it's interpreted by python. If 1.1e-16 is the correct one, however, I'm happy as it'll save me 2 characters :P – user1158 Jul 19 '11 at 21:28

## Perl

$x=1;push@a,$x/=2while$x;die@a[$#a-1];


Python, 29

t=1.
while t+1>1:t/=2
print t


Python3, 28

"true" division sucks, IMO... but I'll use it to cut a character out

t=1
while t+1>1:t/=2
print t

• Your Python3 solution needs parantheses after print. – Skyler Nov 4 '15 at 14:12
• For reference, 10 bytes longer but also possible is: import sys;print sys.float_info.epsilon (documentation) – agtoever Nov 5 '15 at 21:26

# MATLAB, 3 bytes

I'm going to post this purely because it's one of the rare opportunities for MATLAB to be one of if not the shortest. I think it is in the rules for what the question asks.

eps


Granted, not the most ingenious or exciting code though!

• Now, let's golf this down... :) – Sanchises Nov 5 '15 at 21:00

Update

until((==1).(1+))(/2)1


Previous:

For doubles

last$takeWhile(/=0)$iterate(/2)1


For floats

last$takeWhile(/=0)$iterate(/2)(1::Float)

• I think you're looking for until(<=1)(\x->2/x+1)1. Still, though, I can't get ghci to produce anything other than 1.0. – eternalmatt Jul 26 '11 at 1:18

generic
type T is digits <>;function E return T;function E return T is
A,B,C:T:=1.0;begin
loop
A:=A/2.0;B:=C+A;exit when B<=C;end loop;return A;end E;


example usage:

procedure Main is
function F is new E (Float);
function D is new E (Long_Float);
begin
Put_Line ("Epsilon:" & Float'Image (F));
Put_Line ("Epsilon:" & Long_Float'Image (D));
end Main;


output:

Epsilon: 5.96046E-08
Epsilon: 1.11022302462516E-16


# Pyth - 5 bytes

This is invalid to win since Pyth is newer than this challenge, but wanted to put it up anyways.

.IhG1


Invert goes through all positive reals till it finds something that makes the lambda of G equal the second arg. So the code just inverts 1+G till it equals 1.

$MachineEpsilon -> 2.220446049250313*10^-16  • Generally, a Print is used for full programs in Mathematica. – LegionMammal978 Nov 4 '15 at 12:26 • @LegionMammal978 The purpose of Print[ ] in Mathematica is no other than to provide a method for generating output from intermediate results in a CompoundExpression[ ]. It can be normally replaced for the better by Sow[ _, tag] - Reap[_, tag] pairs. And no, you'll not see Print[ ] calls in well designed and functional Mathematica program. – Dr. belisarius Nov 4 '15 at 14:01 • I try to treat Mathematica programs I write here as if they are in a script, where there is no implicit output. – LegionMammal978 Nov 4 '15 at 22:05 • @LegionMammal978 Well, yes. You can try to do that. But it isn't what most Mathematica users do. You can check common programming conventions and pitfalls for example here or here – Dr. belisarius Nov 4 '15 at 22:20 ## PHP, 13 chars For 1 + epsilon > 1, to be true, 1 + epsilon must fit into the number of bits allotted to the mantissa (5 in 5 * 10^4) in floating point numbers. 52 bits are allotted to the mantissa in 64-bit floating point numbers. It follows that 1 + epsilon is 1.000...(52)...0001 (base 2), and that episilon is 0.000...(52)...0001. Calculating this number in decimal gives: <?=pow(2,-52);  You'll notice that negatively exponentiating 2 is the same as repeatedly dividing by 2 -- which is what many of the solutions here do. Bonus, more platform-independent solution (13 chars): <?=sin(pi());  ## Ruby, 18 Just for the sake of completeness: p Float::EPSILON/2  # Mathematica, 21 bytes Print@$MachineEpsilon


# JavaScript ES6 in a REPL,14

Number.EPSILON


# JavaScript ES5 in a REPL,15

Math.pow(2,-52)