Hidden WordScanner
Private scans · No account required

DuckieDai Exact Trig Expressions

Work out a whole trig expression, such as 2sin(x)-cos(2x) or sin(75), and show the answer exactly with square roots of 2, 3, and 6 when it has one.

trigonometry · MIT License

Overview

Work out a whole trig expression, such as 2sin(x)-cos(2x) or sin(75), and show the answer exactly with square roots of 2, 3, and 6 when it has one.

  • Owner-tested on a TI calculator
  • Version 1.0.0
  • 3324 bytes
  • Uses math

Before you run it

  • TI-84 Plus CE Python · No calculator OS or Python App version has been verified.
  • TI Connect CE .py transfer to the Python App; the owner's transfer details were not recorded
  • A number next to a letter or parenthesis is multiplied, so 2x means 2*x and 3sqrt(2) means 3*sqrt(2).
  • Exact forms use whole numbers from -6 to 6 with sqrt(2), sqrt(3), and sqrt(6), over a bottom up to 12.
  • Only numbers, pi, x, the listed functions, + - * / ^, and up to nine levels of parentheses are read; anything else shows an error.

Try the DuckieDai calculator screen

Follow the program's prompts and result flow before downloading.

JavaScript is required for the optional browser simulation. Source viewing and downloading remain available without it.

Simulation boundary: This preview uses a reviewed browser adapter. It does not execute the downloaded Python and does not prove TI-Python or calculator compatibility.

Learn the math

How to work out a trig expression exactly

The idea

Many trig answers are exact numbers made from square roots, such as (sqrt(2)+sqrt(6))/4 for sin 75. A calculator usually shows only a decimal, 0.965926, which hides the exact form you are asked for.

This program works out a whole expression, such as 2sin(x)-cos(2x), and then searches for an exact form made of whole numbers with sqrt(2), sqrt(3), and sqrt(6). Those three roots cover the values of every multiple of 15 degrees, which is where sum and difference identities lead.

The formula

sin(a + b) = sin a cos b + cos a sin b

sin 75 = sin(45 + 30) = (sqrt(2)+sqrt(6))/4

cos(2x) = cos^2 x - sin^2 x

x
a value you type when the expression uses x
^
a power, so 2^3 is 8 and cos(x)^2 is cos squared
~
the decimal value of an exact answer, to six places

Using the program

Worked example

Find sin 75 exactly. Choose 2 for degrees and type sin(75).

sin(45 + 30) = (sqrt(2)/2)(sqrt(3)/2) + (sqrt(2)/2)(1/2) = (sqrt(6)+sqrt(2))/4

The program shows = (sqrt(2)+sqrt(6))/4 and ~ 0.965926.

Type 2sin(x)-cos(2x) and then 45 for x. Since cos 90 is 0, the answer is 2 x sqrt(2)/2, and the program shows = sqrt(2) and ~ 1.414214.

Type tan(90). Cosine is 0 at 90 degrees, so the program shows Undefined.

Tips and common mistakes

Where this comes up

Sum, difference, and double-angle identities are part of the analytic trigonometry unit of precalculus, and exact answers like sin 75 are a common test question.

Exact reviewed bytes

Source code

Download .py

View source below, copy it when JavaScript is available, or download the exact .py bytes.

  1. from math import sin,cos,pi,sqrt
  2. k=1
  3. def z(v):return 0 if abs(v)<1e-12 else v
  4. def S(t):return z(sin(t*k))
  5. def C(t):return z(cos(t*k))
  6. F={"sin":S,"cos":C,"tan":lambda t:S(t)/C(t),"csc":lambda t:1/S(t),
  7. "sec":lambda t:1/C(t),"cot":lambda t:C(t)/S(t),"sqrt":sqrt}
  8. G={"pi":pi}
  9. L="abcdefghijklmnopqrstuvwxyz"
  10. D="0123456789."
  11. def fix(s):
  12. s=s.lower().replace("^","**").replace(" ","")
  13. t=[];i=0
  14. while i<len(s):
  15. j=i+1
  16. if s[i] in L:
  17. while j<len(s) and s[j] in L:j+=1
  18. elif s[i] in D:
  19. while j<len(s) and s[j] in D:j+=1
  20. t.append(s[i:j]);i=j
  21. o=""
  22. for i in range(len(t)):
  23. a=t[i-1] if i else "";b=t[i]
  24. if a and (a[0] in D or a in(")","x","pi")) and (b[0] in L+D or b=="("):o+="*"
  25. o+=b
  26. return o
  27. # Reads only numbers, pi, x, + - * / ** and ( ), and the functions in F.
  28. def ev(s):
  29. t=[];i=0
  30. while i<len(s):
  31. j=i+1
  32. if s[i] in L:
  33. while j<len(s) and s[j] in L:j+=1
  34. elif s[i] in D:
  35. while j<len(s) and s[j] in D:j+=1
  36. elif s[i:i+2]=="**":j=i+2
  37. t.append(s[i:j]);i=j
  38. t.append("");q=[0,0]
  39. def nx():q[0]+=1;return t[q[0]-1]
  40. def add():
  41. v=mul()
  42. while t[q[0]] in("+","-"):v=v+mul() if nx()=="+" else v-mul()
  43. return v
  44. def mul():
  45. v=un()
  46. while t[q[0]] in("*","/"):v=v*un() if nx()=="*" else v/un()
  47. return v
  48. def un():
  49. if t[q[0]]=="-":nx();return -un()
  50. if t[q[0]]=="+":nx();return un()
  51. v=at()
  52. if t[q[0]]=="**":
  53. nx();w=un()
  54. if v<0:
  55. if abs(w-round(w))>1e-9:raise ValueError("math domain error")
  56. w=round(w)
  57. try:v=v**w
  58. except OverflowError:raise ValueError("Too large")
  59. return v
  60. def par():
  61. q[1]+=1
  62. if q[1]>9:raise ValueError("Too many ( )")
  63. v=add()
  64. if nx()!=")":raise ValueError("Missing )")
  65. q[1]-=1;return v
  66. def at():
  67. w=nx()
  68. if w=="(":return par()
  69. if w[:1] in D:return float(w)
  70. if w in F:
  71. if nx()!="(":raise ValueError(w+" needs ( )")
  72. v=par()
  73. if v-v or w=="sqrt" and v<0:raise ValueError("math domain error")
  74. return F[w](v)
  75. if w in G:return G[w]
  76. raise ValueError("Unknown: "+(w or "end"))
  77. v=add()
  78. if t[q[0]]:raise ValueError("Unexpected: "+t[q[0]])
  79. return float(v)
  80. def gcd(a,b):
  81. while b:a,b=b,a%b
  82. return abs(a)
  83. def tx(w,d):
  84. g=d
  85. for n in w:g=gcd(g,n)
  86. w=[n//g for n in w];d//=g;o=""
  87. for n,s in zip(w,("","sqrt(2)","sqrt(3)","sqrt(6)")):
  88. if n:o+=("-" if n<0 else "+" if o else "")+(str(abs(n)) if abs(n)>1 or not s else "")+s
  89. o=o or "0"
  90. if d==1:return o
  91. return ("("+o+")" if "+" in o[1:] or "-" in o[1:] else o)+"/"+str(d)
  92. def nice(v):
  93. r2=sqrt(2);r3=sqrt(3);r6=sqrt(6)
  94. for d in range(1,13):
  95. n=v*d
  96. for a in range(-6,7):
  97. for b in range(-6,7):
  98. p=n-a*r2-b*r3
  99. if abs(p-round(p))<1e-8:return tx((round(p),a,b,0),d)
  100. for c in range(-6,7):
  101. p=n-c*r6
  102. if abs(p-round(p))<1e-8:return tx((round(p),0,0,c),d)
  103. for a in range(-6,7):
  104. for c in range(-6,7):
  105. p=n-a*r2-c*r6
  106. if abs(p-round(p))<1e-8:return tx((round(p),a,0,c),d)
  107. print("EXACT TRIG - DuckieDai\nhiddenwordscanner.com\nType like 2sin(x)-cos(2x)\nBlank entry = quit")
  108. if input("1 Radians 2 Degrees? ").strip()=="2":k=pi/180
  109. while 1:
  110. s=input("Expr? ").strip()
  111. if not s:break
  112. e=s
  113. try:
  114. e=fix(s)
  115. if "x" in e:G["x"]=ev(fix(input(" x = ")))
  116. v=ev(e);r=nice(v)
  117. print("=",r if r else round(v,6))
  118. if r and "sqrt" in r:print("~",round(v,6))
  119. except ZeroDivisionError:print("Undefined")
  120. except Exception as err:print("Got:",e);print("Error:",err)
Filename
TRIGEX.py
Size
3324 bytes
SHA-256
b84ebf24999bf37451d730c756475d447bd2be3e7eb751871449bb4e5108a7f5

Usage

Inputs

  • 1 for radians or 2 for degrees
  • An expression with numbers, pi, x, + - * / ^, parentheses, and sin, cos, tan, csc, sec, cot, and sqrt
  • A value for x when the expression uses x
  • Enter with nothing typed to quit

Outputs

  • The exact answer, such as (sqrt(2)+sqrt(6))/4, followed by its decimal
  • A decimal to six places when there is no exact form
  • Undefined for a division by zero, such as tan(90) in degrees

Units: Angles in radians or degrees, chosen at the start.

Assumptions and limitations

Assumptions

  • A number next to a letter or parenthesis is multiplied, so 2x means 2*x and 3sqrt(2) means 3*sqrt(2).
  • Exact forms use whole numbers from -6 to 6 with sqrt(2), sqrt(3), and sqrt(6), over a bottom up to 12.

Constraints

  • Only numbers, pi, x, the listed functions, + - * / ^, and up to nine levels of parentheses are read; anything else shows an error.
  • A negative number cannot be raised to a fractional power.

Known failures

  • A decimal that happens to be within 0.00000001 of a simple square-root form is shown in that exact form.
  • Very large whole numbers are worked out as decimals, so the last digits can differ from the true value.

Compatibility and review

Review status
Owner-tested on a TI calculator
Tested on
Owner reports personally running this program on a TI-84 Plus CE Python calculator; the test date, OS version, and Python App version were not recorded
What happened
Owner reported pass on the calculator before these changes: the website line was added; expressions are read by the program itself instead of eval, so only math can be typed; exact answers also combine sqrt(2) and sqrt(6), so sin(75) shows (sqrt(2)+sqrt(6))/4; and a negative number to a near whole power uses the whole power. Some error messages are worded differently. Every other step is unchanged; this exact file has not yet been rerun on a calculator.
Dependencies
math
Suggested calculator name
TRIGEX — you can give it another valid, unique name when you transfer it
Desktop test cases
Not available for this interactive-only source

This exact source auto-launches an interactive calculator session, so compatibility evidence comes from the recorded physical-device review rather than an importable desktop fixture.

Transfer and launch

Download the .py file, send it to your calculator with TI Connect CE as TRIGEX or another name, then open the Python App and try the worked example on this page first. This release has no verified calculator OS or Python App version; treat transfer and execution as unverified until independently tested.

Full installation and troubleshooting guide. Check your teacher's or exam's rules before using any program.

Version history

Did a DuckieDai program help with your homework? Every program here is free. If you would like to say thanks, you can leave a tip on Cash App ($duckiedai214) or read what tips pay for.

Independent compatibility resource: Hidden Word Scanner is independent and is not affiliated with or endorsed by Texas Instruments. TI product names describe compatibility only.

Action-count privacy: Successful downloads, preview starts, and preview completions increment same-origin aggregate counters, and so does a Yes or No answer about whether the program worked. Your browser notes that you answered; that note is never sent to the site. No entered values, source text, IP address, account, cookie, device identifier, or fingerprint is stored with these counts.