2

After NumberForm I cannot apply a Sine function. Why?

This works:

sol1 = x /. Solve[x^2 - 3 == 0, x]
Sin[sol1]

Output = $\{-Sin[\sqrt{3}],Sin[\sqrt{3}]\}$

Also this works:

sol2 = x /. Solve[x^2 - 3 == 0, x] // N
Sin[sol2]

Output = $\{-0.987027,0.987027\}$

Then why doesn't this work? Meaning, why is Sin[] not applied to the elements of the list?

sol3 = NumberForm[x /. Solve[x^2 - 3 == 0, x] // N, 6]
Sin[sol3]

Output = $Sin[\{-1.73205,1.73205\}]$

What can I do to make this work?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
GambitSquared
  • 2,311
  • 15
  • 23

2 Answers2

7

Use FullForm to see the differences

sol3 = NumberForm[x /. Solve[x^2-3 == 0, x]//N, 6]
(* {-1.73205,1.73205} *)

This definition includes the NumberForm wrapper that was intended just for printing.

sol3//FullForm
(* NumberForm[List[-1.7320508075688772`,1.7320508075688772`],6] *)

Alternatively, use Information to look at the stored definition

?sol3
(* Global`sol3
sol3=NumberForm[{-1.73205,1.73205},6] *)

Consequently, the input to Sin is neither numeric nor a List of numeric values

Sin[sol3]//FullForm
(* Sin[NumberForm[List[-1.7320508075688772`,1.7320508075688772`],6]] *)

You could Map the Sin to the appropriate level

Map[Sin,sol3,{2}]//FullForm
(* NumberForm[List[-0.9870266449903538`,0.9870266449903538`],6] *)

However, the common approach is to put the wrapper outside the definition

NumberForm[sol32=x/.Solve[x^2-3==0,x]//N,6]//FullForm
(* NumberForm[List[-1.7320508075688772`,1.7320508075688772`],6] *)

However, you have to use NumberForm again if you want the output of Sin to also be formatted with NumberForm

Sin[sol32]//FullForm
(* List[-0.9870266449903538`,0.9870266449903538`] *)

NumberForm[Sin[sol32],6]//FullForm
(* NumberForm[List[-0.9870266449903538`,0.9870266449903538`],6] *)
Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
0
NumberForm[N[x /. Solve[x^2 - 3 == 0, x]], 6]

Sin[%]

(* {-1.73205,1.73205} *)

(* {-0.987027, 0.987027}*)
thils
  • 3,228
  • 2
  • 18
  • 35