0

As far as I know double precision is standard for MatLab but I want to write a program which finds out the smallest possible a (which is in double precision ), so that float(1+a)>1. Is there a function to convert double precision to single precision ?

XPenguen
  • 163
  • 1
  • 8
  • Use the duplicate post but make sure you initialize your variable to `single` to enforce single precision. – rayryeng Jan 15 '16 at 17:53

1 Answers1

5

I think you probably want eps, see:

help eps
1+eps > 1

or to convert a double to single simply cast it in single

A = 1;
singleA = single(A);
matlabgui
  • 5,642
  • 1
  • 12
  • 15
  • Thanks. I should have stated that we aren't allowed to use eps. So I gonna try single(A) and see whether that works. Just one more question. Let´s say a is the smallest positive double value, how can I get the next smallest positive double and so on ? – XPenguen Nov 24 '15 at 11:24
  • a*2 gives you the next smallest I assume? and a*3 gives you the third? – GameOfThrows Nov 24 '15 at 11:31
  • @XPenguen: What you are asking for is exactly the definition of `eps`. As you are not allowed to use it, I recommend to implement the function yourself. – Daniel Nov 24 '15 at 11:53
  • I would try to solve the problem by starting with a number x for which I know single(1+x)=1 and then try the same with x+realmin,x+2*realmin and so on. The problem is, I need a decent approximation for eps, otherwise the loop will take forever. – XPenguen Nov 24 '15 at 12:23
  • "aren't allowed" implies homework. Is this the case? – Carl Witthoft Nov 24 '15 at 13:20
  • You are understanding the floating point precision wrong, x+realmim is x unless x is close to zero. There is no need to approximate eps, it is known. – Daniel Nov 24 '15 at 13:45
  • The single precision floating point format is implemented IEEE-754 standard. You should be able to write a nice function for this by reading the standard or this [wikipedia](https://en.wikipedia.org/wiki/Single-precision_floating-point_format) summary or other references on the web. – patrik Nov 25 '15 at 05:53