'From etoys2.3 of 2 December 2007 [latest update: #1857] on 20 December 2007 at 10:16:44 am'! "Change Set: arilthErrorTweaks-sw Date: 20 December 2007 Author: Scott Wallace A few tweaks to run-time-arithmetic-error handling: * Allow cube-root of negative number. * Insert some missing #translated calls in #reportToUser: error msgs * Put up proper error msg when arg of factorial is inappropriate. "! !Number methodsFor: 'mathematical functions' stamp: 'sw 12/20/2007 10:04'! safeFactorial "Answer the factorial of the receiver." ^ self truncated safeFactorial! ! !Float methodsFor: 'arithmetic' stamp: 'sw 12/20/2007 10:11'! / aNumber "Primitive. Answer the result of dividing receiver by aNumber. Fail if the argument is not a Float. Essential. See Object documentation whatIsAPrimitive." aNumber isZero ifTrue: [^ Preferences eToyFriendly ifTrue: [ScriptingSystem reportToUser: 'division by zero' translated] ifFalse: [(ZeroDivide dividend: self) signal]]. ^ aNumber adaptToFloat: self andSend: #/! ! !Float methodsFor: 'mathematical functions' stamp: 'sw 12/20/2007 09:57'! cubeRoot "Answer the cube root of the receiver." ^ self sign * (self abs raisedTo: 1 / 3) " 8 cubeRoot 0 cubeRoot 1728 cubeRoot 3.14159265 cubeRoot "! ! !Integer methodsFor: 'mathematical functions' stamp: 'sw 12/20/2007 10:05'! safeFactorial "Answer the factorial of the receiver, guarding against bad argument" self = 0 ifTrue: [^ 1]. self > 0 ifTrue: [^ self * (self - 1) factorial]. ScriptingSystem reportToUser: 'Factorial not defined for negative numbers' translated! ! !Player methodsFor: 'scripts-standard' stamp: 'sw 12/20/2007 10:09'! tellAllSiblings: aMessageSelector "Send the given message selector to all my sibling instances, but not to myself" Symbol hasInterned: aMessageSelector ifTrue: [ :sel | self belongsToUniClass ifTrue: [self class allSubInstancesDo: [:anInstance | anInstance isInTrash ifFalse: [ anInstance ~~ self ifTrue: [ anInstance triggerScript: sel ]]]] ifFalse: [(sel ~~ #emptyScript) ifTrue: [ScriptingSystem reportToUser: ('Cannot "tell " ' translated, aMessageSelector, ' to ' translated, self externalName) ]]]! ! !Player methodsFor: 'scripts-standard' stamp: 'sw 12/20/2007 10:10'! tellSelfAndAllSiblings: aMessageSelector "Send the given message selector to all my sibling instances, including myself" Symbol hasInterned: aMessageSelector ifTrue: [ :sel | self belongsToUniClass ifTrue: [self class allSubInstancesDo: [:anInstance | anInstance isInTrash ifFalse: [ anInstance triggerScript: sel ]]] ifFalse: [(sel ~~ #emptyScript) ifTrue: [ScriptingSystem reportToUser: ('Cannot "tell " ' translated, aMessageSelector, ' to ' translated, self externalName) ]]]! ! !Player methodsFor: '*customevents-custom events' stamp: 'sw 12/20/2007 10:10'! triggerScript: aSymbol "Perform the script of the given name, which is guaranteed to exist. However, it's possible that the script may still result in a DNU, which will be swallowed and reported to the Transcript." ^ [[self perform: aSymbol] on: GetTriggeringObjectNotification do: [ :ex | ex isNested ifTrue: [ ex pass ] ifFalse: [ ex resume: self ]]] on: MessageNotUnderstood do: [:ex | ScriptingSystem reportToUser: (String streamContents: [:s | s nextPutAll: self externalName; nextPutAll: ': exception in script ' translated; print: aSymbol; nextPutAll: ' : '; print: ex]). ex return: self "ex pass"]! ! !Player methodsFor: '*green' stamp: 'sw 12/20/2007 10:08'! setLocation: val "Set the receiver's location; expected to be called with a point argument" | aCostume | (val isKindOf: Point) ifFalse: [^ ScriptingSystem reportToUser: 'Expected a Point but instead got ' translated, val printString]. (aCostume _ self costume) isInWorld ifFalse: [^ self]. aCostume isWorldOrHandMorph ifTrue: [^ self]. aCostume owner isHandMorph ifTrue: [^ self]. ^ aCostume x: val x y: val y! ! !StandardScriptingSystem methodsFor: 'utilities' stamp: 'sw 12/20/2007 10:06'! tableOfNumericFunctions "Answer an array of triplets." " English on tile selector English balloon help" ^ #( (abs abs 'absolute value') (arcTan arcTan 'angle, in radians, whose tangent is the argument') (cos cos 'trigonometric cosine, argument in radians') (cubed cubed 'the argument times itself, times itself again') (cubeRoot cubeRoot 'cube root of the argument') (degreeArcTan degreeArcTan 'angle, in degrees, whose tangent is the argument') (degreeCos degreeCos 'trigonometric cosine, argument in degrees') (degreeSin degreeSin 'trignometric sine, argument in degrees') (degreeTan degreeTan 'trigonometric tangent, argument in degrees') (degreesToRadians degreesToRadians 'the number of degrees equivalent to the argument which is assumed to be expressed in radians') (exp exp 'exponential (e to the power of the argument)') (factorial safeFactorial 'the product of all the whole numbers between 1 and the argument') (ln safeLn 'natural logarithm') (log safeLog 'logarithm, base 10') (negativeOf negated 'the negative of the argument') (radiansToDegrees radiansToDegrees 'the number of radians equivalent to the argument, which is expressed in degrees.') (random random 'a randomly chosen integer between 1 and the argument') (rounded rounded 'the integer closest to the argument.') (sin sin 'trigonometric sine, argument in radians') (squared squared 'the argument multiplied by itself') (squareRoot safeSquareRoot 'square root of the argument') (tan tan 'trigonometric tangent, argument in radians')) translatedNoop " (raisedto raisedTo: 'raised to the power') "! !