Contents
Articles
Behaviors
Books
Director News
Director Web Sites
FAQ
Games
Mailing Lists
News Groups
Project Examples
Reviews
Software
Tools
Useful Web Sites
Utilities
Xtras

Don't miss these
Easy Help
Triming spaces from string
SaveScripts Xtra
Volume Slider
Pendulum Rotate-Alphamania
Set FrameRate of a Animated Gif Cast Member
Check For Asset
The Complete Uninstall
Effector Set 1 Behavior Support
myMailTo
 

 

 

Behavior Fraction Scripts

Added on 6/30/2000

 

Compatibilities:
D7 D8 Mac PC Script Shockwave

Rating:

Author: BarrySwan (website)

Fraction code

Download PC Source    Download Mac Source
--fAdd
on fAdd tFraction1, tFraction2
  -- Adds two given fractions
  tWorkingFraction1 = fMakeImproper(tFraction1)
  tWorkingFraction2 = fMakeImproper(tFraction2)
  -- Convert fractions to use a common denominator
  tNewDenominator = tWorkingFraction1.denominator * tWorkingFraction2.denominator  
  tWorkingFraction1 = fChangeDenominator(tWorkingFraction1, tNewDenominator)
  tWorkingFraction2 = fChangeDenominator(tWorkingFraction2, tNewDenominator)
  -- Add them together and make into a proper fraction
  tCombinedNumerator = tWorkingFraction1.numerator + tWorkingFraction2.numerator
  tSumFraction = fNew(tCombinedNumerator, tNewDenominator)
  tSumFraction = fMakeProper(tSumFraction)
  -- Return the total
  return tSumFraction
end


--fAddAndSimplify
on fAddAndSimplify tFraction1, tFraction2  
  -- Adds two fractions and simplifies the result
  tAddedFraction = fAdd(tFraction1, tFraction2)
  tSimplifiedFraction = fSimplify(tAddedFraction)
  -- Return the final fraction
  return tSimplifiedFraction
end


--fAreEqual
on fAreEqual tFraction1, tFraction2
  -- Returns whether two fractions are equal
  tComparison = fCompare(tFraction1, tFraction2)
  if tComparison = #equal then tAreEqual = TRUE
  else tAreEqual = FALSE
  -- Returns TRUE or FALSE
  return tAreEqual
end


--fChangeDenominator
on fChangeDenominator tFraction, tNewDenominator  
  -- Converts a fraction into using given denominator
  -- Returns -1 instead of the converted fraction if conversion is impossible
  -- Check it can be converted
  tCanChange = TRUE
  -- Check that numerator can be evenly divided into the new denominator
  tScale = (tFraction.denominator + 0.0) / tNewDenominator
  tNewNumerator = integer(tFraction.numerator / tScale)
  if (tFraction.numerator / tScale) <> tNewNumerator then
    tCanChange = FALSE
  end if
  if tCanChange = TRUE then
    -- Can convert so do so
    tConvertedFraction = fCopy(tFraction)
    tConvertedFraction.numerator = tNewNumerator
    tConvertedFraction.denominator = tNewDenominator
  else
    -- Cannot convert, so return -1
    tConvertedFraction = -1
  end if
  -- Return converted (or otherwise) fraction
  return tConvertedFraction
end


--fCompare
on fCompare tFraction1, tFraction2
  -- Compares the second fraction against the first
  tFraction1Value = fGetDecimalValue(tFraction1)
  tFraction2Value = fGetDecimalValue(tFraction2)
  if tFraction1Value = tFraction2Value then tComparison = #equal
  else if tFraction2Value < tFraction1Value then tComparison = #lessthan
  else tComparison = #greaterthan
  -- Returns the comparison
  return tComparison
end


--fCopy
on fCopy tFraction
  -- Duplicates the fraction
  return duplicate(tFraction)
end


--fFractionToString
on fFractionToString tFraction
  -- Make a fraction variable into a string
  if tFraction.whole <> 0 then tString = string(tFraction.whole) & " "
  else tString = EMPTY
  tString = tString & string(tFraction.numerator) & "/" & string(tFraction.denominator)
  -- Return the string
  return tString
end


--fGetCommonDenominators
on fGetCommonDenominators tFraction1, tFraction2, tUpperLimit  
  tlValidDenominators = []
  tlFraction1ValidDenominators = []
  tlFraction2ValidDenominators = []
  -- Get lists of valid denominators for fractions
  repeat with a = 1 to tUpperLimit
    if fIsDenominator(tFraction1, a) then
      add tlFraction1ValidDenominators, a
    end if
    if fIsDenominator(tFraction2, a) then
      add tlFraction2ValidDenominators, a
    end if
  end repeat
  -- Compare lists and only return values in both
  repeat with a = 1 to count(tlFraction1ValidDenominators)
    if getone(tlFraction2ValidDenominators, tlFraction1ValidDenominators[a]) > 0 then
      add tlValidDenominators, tlFraction1ValidDenominators[a]
    end if
  end repeat  
  -- Return list of valid denominators
  return tlValidDenominators
end


--fGetDecimalValue
on fGetDecimalValue tFraction
  -- Returns the decimal value of a fraction
  if tFraction.denominator <= 0 then return -1
  tDecimalValue = tFraction.whole + (tFraction.numerator + 0.0) / tFraction.denominator
  -- Returns the decimal value
  return tDecimalValue
end


--fGetDenominators
on fGetDenominators tFraction, tUpperLimit  
  tlValidDenominators = []
  -- Get a list of valid denominators for fraction
  repeat with a = 1 to tUpperLimit
    if fIsDenominator(tFraction, a) then
      add tlValidDenominators, a
    end if
  end repeat
  -- Return list of valid denominators
  return tlValidDenominators
end


--fGetLowestCommonDenominator
on fGetLowestCommonDenominator tFraction1, tFraction2  
  -- Finds the lowest common denominator
  tMaximumDenominator = tFraction1.denominator * tFraction2.denominator
  repeat with a = 1 to tMaximumDenominator
    if fIsDenominator(tFraction1, a) and fIsDenominator(tFraction2, a) then
      tLowestCommonDenominator = a
      exit repeat
    end if
  end repeat
  -- Return the lowest common denominator
  return tLowestCommonDenominator
end


--fGetLowestDenominator
on fGetLowestDenominator tFraction  
  -- Find lowest denominator by simply starting at 1 and working up to current denominator
  tLowestDenominator = tFraction.denominator
  repeat with a = 1 to tFraction.denominator
    if fChangeDenominator(tFraction, a) <> -1 then
      tLowestDenominator = a
      exit repeat
    end if
  end repeat
  -- Return the lowest found denominator
  return tLowestDenominator
end


--fGetType
on fGetType tFraction
  -- Gets the type of the given fraction
  if fIsProper(tFraction) then tFractionType = #proper
  else
    if fIsImproper(tFraction) then tFractionType = #improper
    else tFractionType = #mixed
  end if
  -- Return fraction type
  return tFractionType
end


--fGetWhole
on fGetWhole tFraction
  -- Gets the whole part of any sort of fraction
  tWorkingFraction = fMakeProper(tFraction)
  tWhole = tWorkingFraction.whole
  -- Return just the whole part
  return tWhole
end


--fIsCommonDenominator
on fIsCommonDenominator tFraction1, tFraction2, tDenominator  
  -- Checks whether a denominator is valid for two fractions
  tIsCommonDenominator = TRUE
  if fChangeDenominator(tFraction1, tDenominator) = -1 then tIsCommonDenominator = FALSE
  if fChangeDenominator(tFraction2, tDenominator) = -1 then tIsCommonDenominator = FALSE
  -- Return TRUE or FALSE
  return tIsCommonDenominator
end


--fIsDenominator
on fIsDenominator tFraction, tDenominator  
  -- Checks whether a denominator is a valid denominator for a fraction
  if fChangeDenominator(tFraction, tDenominator) = -1 then tIsDenominator = FALSE
  else tIsDenominator = TRUE
  -- Returns TRUE or FALSE
  return tIsDenominator
end


--fIsImproper
on fIsImproper tFraction  
  -- Checks whether a fraction is improper
  tIsImproper = TRUE
  if tFraction.whole > 0 then tIsImproper = FALSE
  if tFraction.numerator < tFraction.denominator then tIsImproper = FALSE
  -- Returns TRUE or FALSE
  return tIsImproper


--fIsMixed
on fIsMixed tFraction  
  -- Checks whether a fraction is mixed
  tIsMixed = TRUE
  if tFraction.numerator < tFraction.denominator then
    tIsMixed = FALSE
  else
    if tFraction.whole = 0 then
      tIsMixed = FALSE
    end if
  end if
  -- Returns TRUE or FALSE
  return tIsMixed
end


--fIsProper
on fIsProper tFraction  
  -- Checks whether a fraction is proper
  tIsProper = TRUE
  if tFraction.numerator >= tFraction.denominator then tIsProper = FALSE
  -- Returns TRUE or FALSE
  return tIsProper
end


--fJustFraction
on fJustFraction tFraction  
  -- Returns a fraction with top limit of 1 unit
  tJustFraction = fMakeProper(tFraction)
  if (tJustFraction.numerator = 0) and (tJustFraction.whole > 0) then
    tJustFraction.numerator = tJustFraction.denominator
  end if
  tJustFraction.whole = 0
  -- Returns just the fraction
  return tJustFraction
end


--fMakeFraction
on fMakeFraction tDecimalValue  
  -- Convert a decimal value into a fraction and simplify the result
  if tDecimalValue <0 then
    tNewFraction = -1
  else
    tDecimalValue = value(string(tDecimalValue)) * 10000
    tNewFraction = fMakeProper(fSimplify(fNew(tDecimalValue, 10000)))
  end if
  -- Return the fraction (or error)
  return tNewFraction
end


--fMakeImproper
on fMakeImproper tFraction  
  -- Make into improper fraction
  tImproperFraction = fNew()
  tNumerators = tFraction.numerator + tFraction.whole * tFraction.denominator
  tImproperFraction.numerator = tNumerators
  tImproperFraction.denominator = tFraction.denominator
  tImproperFraction.whole = 0
  -- Return converted fraction
  return tImproperFraction
end


--fMakeMixed
on fMakeMixed tFraction, tWholeUsed  
  -- Make into a mixed fraction (Returns -1 if impossible)
  tMixedFraction = fCopy(tFraction)
  tImproperFraction = fMakeImproper(tFraction)
  tNumeratorsToDelete = tWholeUsed * tFraction.denominator
  -- Check there are enough numerators to subtract from
  if tNumeratorsToDelete <= tFraction.numerator then
    tMixedFraction = fMakeProper(tFraction)
  else
    tMixedFraction.numerator = tMixedFraction.numerator - tNumeratorsToDelete
    tMixedFraction.whole = tWholeUsed
  end if
  -- Return converted fraction
  return tMixedFraction
end


--fMakeProper
on fMakeProper tFraction  
  -- Make into proper fraction
  tProperFraction = fNew()
  tNumerators = tFraction.numerator + tFraction.whole * tFraction.denominator
  tProperFraction.numerator = tNumerators mod tFraction.denominator
  tProperFraction.denominator = tFraction.denominator
  tProperFraction.whole = tNumerators / tFraction.denominator
  -- Return converted fraction
  return tProperFraction
end


--fNew
on fNew tNumerator, tDenominator, tWhole
  -- Initialise and return a fraction property list
  if voidP(tWhole) then tWhole = 0
  if (voidP(tDenominator)) and (voidP(tNumerator)) then tDenominator = 1
  if (voidP(tDenominator)) and (not(voidP(tNumerator))) then tDenominator = tNumerator
  if voidP(tNumerator) then tNumerator = 0
  -- If values are not integers then call fMakeFraction instead
  if (tNumerator <> integer(tNumerator)) or (tDenominator <> integer(tDenominator)) or (tWhole <> integer(tWhole)) then
    tNewFraction = fMakeFraction(tNumerator / tDenominator + tWhole * tDenominator)
  else
    tNewFraction = [#numerator: integer(tNumerator), #denominator: integer(tDenominator), #whole: integer(tWhole)]
  end if
  -- Return fraction property list
  return tNewFraction
end


--fRemoveWhole
on fRemoveWhole tFraction
  -- Removes the whole part from any type of fraction
  tJustFraction = fMakeProper(tFraction)
  tJustFraction.whole = 0
  -- Return just the fractional part
  return tJustFraction
end


--fSimplify
on fSimplify tFraction
  -- Uses the other routines to simplify the fraction
  tLowestDenominator = fGetLowestDenominator(tFraction)
  tSimplifiedFraction = fChangeDenominator(tFraction, tLowestDenominator)
  -- Return the simplified fraction
  return tSimplifiedFraction
end


--fStringToFraction
on fStringToFraction tString
  -- Make a string into a fraction 'variable type'
  tFraction = fNew()
  if tString contains " " then
    tFraction.whole = value(tString.char[1..(offset(" ", tString) - 1)])
    tString = tString.char[(offset(" ", tString) + 1)..length(tString)]
  else
    tFraction.whole = 0
  end if
  tFraction.numerator = value(tString.char[1..(offset("/", tString) - 1)])
  tFraction.denominator = value(tString.char[(offset("/", tString) + 1)..length(tString)])
  -- Return the fraction
  return tFraction
end


--fSubtract
on fSubtract tFraction1, tFraction2
  -- Subtracts two given fractions
  tWorkingFraction1 = fMakeImproper(tFraction1)
  tWorkingFraction2 = fMakeImproper(tFraction2)
  -- Convert fractions to use a common denominator
  tNewDenominator = tWorkingFraction1.denominator * tWorkingFraction2.denominator  
  tWorkingFraction1 = fChangeDenominator(tWorkingFraction1, tNewDenominator)
  tWorkingFraction2 = fChangeDenominator(tWorkingFraction2, tNewDenominator)
  -- Subtract them and make into a proper fraction
  tSubtractedNumerator = tWorkingFraction1.numerator - tWorkingFraction2.numerator
  -- If numerator is no longer positive return an error
  if tSubtractedNumerator < 0 then
    tSumFraction = -1
  else
    tSumFraction = fNew(tSubtractedNumerator, tNewDenominator)
    tSumFraction = fMakeProper(tSumFraction)
  end if
  -- Return the total (or error code)
  return tSumFraction
end

 


Contact

MMI
36 South Court Sq
Suite 300
Newnan, GA 30263
USA

Send e-mail