1

if I write in matlab

cos(pi/4)

I get this form:

0.7071

when I'd prefer getting it actual value of (2)^(1/2)/2 to write formulas more clearly. is there a way to do what I ask?

user1834153
  • 330
  • 5
  • 20
  • 3
    Without the Matlab symbolic toolbox then the floating-point number approximated by `0.7071` is as good as you will get -- of course, behind the scenes Matlab is using the full double-precision representation of the number, and by default displaying only the first 4 significant digits. You're not getting the display format and the internal value mixed up are you ? – High Performance Mark Jan 03 '14 at 17:35
  • perhaps OP would like to try `> format long` – vish Jan 03 '14 at 17:37
  • no I'm not,but since I have the symbolic toolbox I wonder why I get that form instead of a more clear one – user1834153 Jan 03 '14 at 17:38
  • @user3149593: Because it's *considerably* more computationally expensive to evaluate symbolic math expressions in general and thus you have to explicitly use it. – horchler Jan 03 '14 at 17:50
  • I don't see why you find `cos(pi/4)` unclear. The result in Matlab is a numerical approximation, sure, after all you are using a programming language specialized for numeric computations. But the *code* in which you write this expression is clear as day, it says exactly what it means. – A. Donda Jan 03 '14 at 19:36
  • @A.Donda I'd kepp cos(pi/4) if I could,it's the number that I don't want – user1834153 Jan 03 '14 at 20:37

1 Answers1

4

You can you use the Symbolic Math toolbox that comes with many installs of Matlab:

theta = sym(pi)/4;
cos(theta)

which returns

ans =

2^(1/2)/2
horchler
  • 18,384
  • 4
  • 37
  • 73
  • that's really a strange way to solve it,but it works greatly,thanks a lot – user1834153 Jan 03 '14 at 17:39
  • @user3149593 you should be aware that the result is not anymore an integer, double, or .... it is a sym type. – NKN Jan 03 '14 at 17:41
  • @NKN doing eval() I can get it back to double if needed righ? – user1834153 Jan 03 '14 at 17:43
  • 3
    @user3149593: Not strange at all. Matlab, like most programming languages, uses floating-point representation. This value and many others have no exact form in floating-point. To convert the numerical output of a symbolic calculation back to double precision floating-point, use the `double` function. – horchler Jan 03 '14 at 17:44