Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

Is there a preferred way of converting an Integer to a Real?

What I'm using at the moment is x + 0.0.

share|improve this question

marked as duplicate by Michael E2, Mr.Wizard Jul 23 at 3:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

12  
N is what I use. – Michael E2 Oct 15 '13 at 19:20
4  
@MichaelE2 your chance to post a canonical answer using one character ;-) – Yves Klett Oct 16 '13 at 5:24
    
Although the answer is trivial, there is no one-hit wonder on google or in the documentation. The first immediately useful hit is reference.wolfram.com/legacy/v2/contents/3.1.2.pdf, which is already running under "legacy", so I would vote against closure as long as @MichaelE2 posts his comment as answer. – Yves Klett Oct 16 '13 at 7:57
    
@yvesklett I was about to vote to close, but you're right, unfortunately. I have been trying to find an answer to this question as if I were a newbie. There just doesnt seem to be an easy and intuitive way a new user could arrive at the answer, at least not from the online documentation. – Sjoerd C. de Vries Oct 16 '13 at 9:33
    
@SjoerdC.deVries my first reflex was also to close (as in quite a few cases these last days). It may be a duplicate, but in fact this title question and the straightforward answer may be even more useful for newbies. – Yves Klett Oct 16 '13 at 11:29
up vote 10 down vote accepted

N


A one-character answer is disallowed by SE, so I will expand. N is mostly what I use. If I have an expression like $2 x + 3$, I sometimes write it 2. x + 3. in Mathematica; then if x is numeric, whether it happens to be an Integer or not, the expression will always be Real or Complex.

share|improve this answer
    
Indeed, the last part of the statement is always true, my bad. Well... actually, if x is something completely different..... :) – István Zachar Oct 16 '13 at 11:17
    
Michael, it seems to me that this question may be considered a duplicate of (7564). Since you have the Accepted answer here what do you think? – Mr.Wizard Jul 23 at 1:33
1  
@Mr.Wizard Yes, they seem sufficiently close to be called duplicates. – Michael E2 Jul 23 at 2:54
Head /@ {23, 23*1, 23/1, 23 + 0, 23., 23*1., 23/1., 23 + 0.}

{Integer, Integer, Integer, Integer, Real, Real, Real, Real}

share|improve this answer

This also

{Head[1], Head[1 // N]}

{Integer, Real}

share|improve this answer

Real is the best way to convert an integer into a real as shown by the following.

AbsoluteTiming[Do[
  i = 1;
  Real[i];
  ,{j,1,10^6}]]
(* {1.881108,Null}

Knowing that the part i = 1; takes some time by itself.

AbsoluteTiming[Do[ 
  i = 1;
  , {j, 1, 10^6}]]
(* {1.216070, Null}

it means that converting one million 1 to 1. takes 0.665038 s.

All other ways are significantly longer. The method using N being a very slow method.

AbsoluteTiming[Do[
  i = 1;
  i*1.0;
  , {j, 1, 10^6}]]
(* {2.042117, Null} *)

AbsoluteTiming[Do[
  i = 1;
  i*1.;
  , {j, 1, 10^6}]]
(* {2.043117, Null} *)

AbsoluteTiming[Do[
  i = 1;
  i + 0.0;
  , {j, 1, 10^6}]]
(* {2.080119, Null} *)

AbsoluteTiming[Do[
  i = 1;
  i + 0.;
  , {j, 1, 10^6}]]
(* {2.082119, Null} *)

AbsoluteTiming[Do[
  i = 1;
  i^1.0;
  , {j, 1, 10^6}]]
(* {3.199183, Null} *)

AbsoluteTiming[Do[
  i = 1;
  i^1.;
  , {j, 1, 10^6}]]
(* {3.207183, Null} *)

AbsoluteTiming[Do[
  i = 1;
  N[i];
  , {j, 1, 10^6}]]
(* {3.459198, Null} *)

AbsoluteTiming[Do[
  i = 1;
  i/1.;
  , {j, 1, 10^6}]]
(* {3.626207, Null} *)

AbsoluteTiming[Do[
  i = 1;
  i/1.0;
  , {j, 1, 10^6}]]
(* {3.662209, Null} *)

It means that converting one million 1 to 1. by N takes 2.24313 s, thus 3.37293 times longer than using Real. It can have a significant effect in some programs.

share|improve this answer
1  
This has some good content but the formatting hurts the eyes. – Edmund Jul 23 at 0:30
    
Because this is your 1st post, I have edited your answer so it complies with the formatting guidelines of this site. I hope you will take some time to study those guidelines and adhere to them in future posts. – m_goldberg Jul 23 at 1:11
2  
In version 10.1.0 Real[1] does not evaluate further. In other words it does not change an Integer into a Real. Is this different on your system? – Mr.Wizard Jul 23 at 1:28
2  
@Mr.Wizard Ditto for 10.4.1, 9.0.1, and 8.0.4. – Michael E2 Jul 23 at 2:57
1  
Well I guess this answer is incorrect. I tested in version 3, 5, 6, 7, 8, 9, 10 – happy fish Jul 23 at 6:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.