bazelbuild / starlark

Starlark Language
Apache License 2.0
2.48k stars 163 forks source link

Add `**` power operator #190

Open tetromino opened 3 years ago

tetromino commented 3 years ago

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

adonovan commented 3 years ago

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

vraj015 commented 3 months ago

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

'''

adonovan commented 3 months ago

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