starlark icon indicating copy to clipboard operation
starlark copied to clipboard

Add `**` power operator

Open tetromino opened this issue 4 years ago • 3 comments

Following the example of Python. Motivation: https://github.com/bazelbuild/bazel-skylib/issues/282

tetromino avatar Apr 15 '21 23:04 tetromino

I see no compelling reason for this feature. For powers of 2, use a shift. For anything else, use the math package.

adonovan avatar May 06 '21 15:05 adonovan

Hello Adonovan can you please share the example of how to use Shift? I am trying to achieve below mention code. FYI this code is part of a telegraf conf file

[[processors.starlark]] source = ''' def apply(metric): # Conversion for A1 using the formula 10^(A1_raw - 5) as a float if "A1_raw" in metric.fields: metric.fields["A1"] = float(10 ** (metric.fields["A1_raw"] - 5))

# Conversion for A2 using the formula 10^(A2_raw/100 - 10) as a float
if "A2_raw" in metric.fields:
    metric.fields["A2"] = float(10 ** ((metric.fields["A2_raw"] / 100) - 10))
    
return metric

'''

vraj015 avatar Aug 10 '24 00:08 vraj015

can you please share the example of how to use Shift?

Use << for integer operands, and math.pow for non-integers:

$ starlark
Welcome to Starlark (go.starlark.net)
>>> 1 << 8
256
>>> math.pow(2.0, 8.0)
256.0

adonovan avatar Aug 11 '24 15:08 adonovan