问题
Bazel (0.26.0) does not support float types as you can read here.
However, I would like to compute some magic (floating-point) number store them in string
as shown here:
def magicNumber():
fileStr = ""
count = 200
for i in range(0, count-1):
v = i / count
fileStr += str(v) + " "
return fileStr
I want to use Bazel-only features to achieve this. It is clear to me that I could also place my computation for instance in batch/shell script, but I want stick to Bazel-only features. Any ideas how to achieve this?
回答1:
It's unclear why you'd want to do that. Integers should usually be enough.
If what you want is the sequence "0.0 0.005 0.01 0.015 0.02... 0.995", you can do that like this:
def magic():
fileStr = ""
for i in range(0, 1000, 5):
s = ("000" + str(i))[-3:] # Add leading 0s
fileStr += "0.{} ".format(s.rstrip('0'))
return fileStr
Since the strings are not mutable, +=
on a string will copy it. Doing that in a loop is not recommended for performance (quadratic complexity). Instead, you may append data to an array and join it at the end. It's the same result, except that it doesn't have the trailing space:
def magic():
data = []
for i in range(0, 1000, 5):
s = ("000" + str(i))[-3:]
data.append("0." + s.rstrip('0'))
return " ".join(data)
来源:https://stackoverflow.com/questions/56374262/how-to-emulate-float-double-real-in-bazel