(→‎Workaround: added new one)
Line 39: Line 39:
 
The following code accepts negative numbers with decimals:
 
The following code accepts negative numbers with decimals:
  
[[File:() Times () Workaround.png]]
+
<scratchblocks>
 +
delete (all v) of [num1 numbers v] //setup
 +
delete (all v) of [num2 numbers v]
 +
delete (all v) of [product digits v]
 +
set [product v] to [0]
 +
set [dec pos 1 v] to [0]
 +
set [dec pos 2 v] to [0]
 +
ask [num1] and wait
 +
if <(answer) < [0]>
 +
set [count v] to [1]
 +
set [no 1 negative v] to [y]
 +
else
 +
set [count v] to [0]
 +
set [no 1 negative v] to [n]
 +
end
 +
repeat (length of (answer))
 +
change [count v] by (1)
 +
if <not <(letter (count) of (answer)) = [.]>>
 +
add (letter (count) of (answer)) to [num1 numbers v]
 +
else
 +
set [dec pos 1 v] to ((length of (answer)) - (count))
 +
end
 +
end
 +
ask [num2] and wait
 +
if <(answer) < [0]>
 +
set [count v] to [1]
 +
set [no 2 negative v] to [y]
 +
else
 +
set [count v] to [0]
 +
set [no 2 negative v] to [n]
 +
end
 +
repeat (length of (answer))
 +
change [count v] by (1)
 +
if <not <(letter (count) of (answer)) = [.]>>
 +
add (letter (count) of (answer)) to [num2 numbers v]
 +
else
 +
set [dec pos 2 v] to ((length of (answer)) - (count))
 +
end
 +
end
 +
set [num1 v] to (num1 numbers)
 +
set [num2 v] to (num2 numbers)
 +
repeat (num1) //start
 +
change [product v] by (num2)
 +
end
 +
set [decimal position v] to ((dec pos 1) + (dec pos 2))
 +
set [count v] to [0]
 +
repeat (length of (product))
 +
change [count v] by (1)
 +
add (letter (count) of (product)) to [product digits v]
 +
end
 +
if <not <(decimal position) = [0]>>
 +
insert [.] at ((length of [product digits v]) - ((decimal position) - (1))) of [product digits v]
 +
end
 +
if <<<(no 1 negative) = [y]> or <(no 2 negative) = [y]>> and <not <<(no 1 negative) = [y]> and <(no 2 negative) = [y]>>>>
 +
insert [-] at (1 v) of [product digits v]
 +
end
 +
set [product v] to (product digits)
 +
</scratchblocks>
  
 
==Example Uses==
 
==Example Uses==

Revision as of 14:59, 14 May 2012

() * ()
() * ().png
Category Operators
Type Reporter

The () * () block is an Operators block and a Reporter block. The block multiplies the two values and reports the result.

The numbers can be typed directly into the block, or Reporter blocks can be used instead.

This block can be stacked inside itself - this can be used to fit more numbers in or calculate exponents.

Scientific Numbers

Numbers in scientific notation in variables (such as 3e3) can be converted to a normal number by preforming any mathematical function on it, such as multiplying. So if a variable named "number" has a value of 3e3 and you want to use it in other scripts, you can change it by doing the following:

((1) * (number))

It will then report "3000".

Workaround

With natural numbers, this block can be replicated with the following code, assuming a is the first number and b is the second number:

set [product v] to [0]
repeat (b)
change [product v] by (a)

The following code works for all cases (with the conditional). It basically divides by the reciprocal, the equivalent of multiplying.

if<(a) = (0)>
set [product v] to [0]
else
set [product v] to ((a)/((1)/(b)))
end

The following code accepts negative numbers with decimals:

delete (all v) of [num1 numbers v] //setup
delete (all v) of [num2 numbers v]
delete (all v) of [product digits v]
set [product v] to [0]
set [dec pos 1 v] to [0]
set [dec pos 2 v] to [0]
ask [num1] and wait
if <(answer) < [0]>
set [count v] to [1]
set [no 1 negative v] to [y]
else
set [count v] to [0]
set [no 1 negative v] to [n]
end
repeat (length of (answer))
change [count v] by (1)
if <not <(letter (count) of (answer)) = [.]>>
add (letter (count) of (answer)) to [num1 numbers v]
else
set [dec pos 1 v] to ((length of (answer)) - (count))
end
end
ask [num2] and wait
if <(answer) < [0]>
set [count v] to [1]
set [no 2 negative v] to [y]
else
set [count v] to [0]
set [no 2 negative v] to [n]
end
repeat (length of (answer))
change [count v] by (1)
if <not <(letter (count) of (answer)) = [.]>>
add (letter (count) of (answer)) to [num2 numbers v]
else
set [dec pos 2 v] to ((length of (answer)) - (count))
end
end
set [num1 v] to (num1 numbers)
set [num2 v] to (num2 numbers)
repeat (num1) //start
change [product v] by (num2)
end
set [decimal position v] to ((dec pos 1) + (dec pos 2))
set [count v] to [0]
repeat (length of (product))
change [count v] by (1)
add (letter (count) of (product)) to [product digits v]
end
if <not <(decimal position) = [0]>>
insert [.] at ((length of [product digits v]) - ((decimal position) - (1))) of [product digits v]
end
if <<<(no 1 negative) = [y]> or <(no 2 negative) = [y]>> and <not <<(no 1 negative) = [y]> and <(no 2 negative) = [y]>>>>
insert [-] at (1 v) of [product digits v]
end
set [product v] to (product digits)

Example Uses

In many projects, numbers must be multiplied - this block will do the job.

Some common uses for the () * () block:

  • Scripts in calculator simulations
  • Multiplying lists of numbers
  • Math formulas

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.