• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Decimal Place Limiting
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Decimal Place Limiting


  • Subject: Re: Decimal Place Limiting
  • From: deivy petrescu <email@hidden>
  • Date: Wed, 2 Apr 2008 22:25:03 -0400


On Apr 1, 2008, at 17:20, John Amerkhanian wrote:
I wrote a small script to solve quadratic equations and it works fine until I start putting in larger numbers. I first noticed this when I put in 2, 4, 3 for a, b, c and I realized this was because my variable top1 has too many decimal places. How can I limit its decimal places to say... four?

Heres the script:

repeat
display dialog "Enter the coefficient A:" default answer "" buttons "Next" default button 1

set A to the text returned of the result as number

display dialog "Enter the coefficient B:" default answer "" buttons "Next" default button 1

set B to the text returned of the result as number

display dialog "Enter the coefficient C:" default answer "" buttons "Solve" default button 1

set C to the text returned of the result as number


set top1 to (B * B - 4 * A * C)

set top to top1 ^ (1 / 2)

set root1 to (-B + top) / (2 * A)
set root2 to (-B - top) / (2 * A)

display dialog "The solutions are: " & root1 & ", " & root2 & "" buttons {"Ok", "Do it Again"} default button 1
set theResult to the result as list
if theResult is equal to {"Ok"} then exit repeat
end repeat

John
As people already pointed out to you, there will be errors when you take the square root of a negative numbers.
I tweaked the script a bit, no this script will take care of complex roots.
Here is the script:


_____
findroots()

on findroots()
set nmbr to 65
set lista to {}
set imaginary to 0
repeat
set ddlg to (display dialog "Enter the coefficient " & (ASCII character nmbr) & ":" default answer "" buttons {"OK", "Cancel"} default button 1)

if button returned of ddlg is "Cancel" then error number -128
try
set end of lista to the text returned of ddlg as number
on error
display dialog "You have to enter a *number*"
findroots()
end try
set nmbr to nmbr + 1
if (count of lista) = 3 then exit repeat
end repeat
set {A, B, C} to lista

set delta to (B ^ 2 - 4 * A * C)
if delta < 0 then
set imaginary to 1
set delta to -1 * delta
end if

set root1 to {|real|:(-B + (1 - imaginary) * (delta ^ (0.5))) / (2 * A), imaginary:imaginary * (delta ^ (0.5)) / (2 * A)}
set root2 to {|real|:(-B - (1 - imaginary) * (delta ^ (0.5))) / (2 * A), imaginary:(-1) * imaginary * (delta ^ (0.5)) / (2 * A)}

set resultado to (display dialog "The solutions are: " & return & "real part of root 1: " & tab & |real| of root1 & return & "imaginary part of root 1: " & imaginary of root1 & return & return & "real part of root 2: " & tab & |real| of root2 & return & "imaginary part of root 2: " & imaginary of root2 buttons {"Ok", "Do it Again"} default button 2)
if button returned of resultado is "Do it Again" then findroots()
end findroots
____________



Deivy _______________________________________________ Do not post admin requests to the list. They will be ignored. AppleScript-Users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden
References: 
 >Decimal Place Limiting (From: John Amerkhanian <email@hidden>)

  • Prev by Date: Re: Fixed it!
  • Next by Date: Re: Safari script
  • Previous by thread: Re: Safari script
  • Next by thread: re: launching firefox
  • Index(es):
    • Date
    • Thread