如何在Bazel中模拟float/double/real? [英] How to emulate float/double/real in Bazel?

查看:81
本文介绍了如何在Bazel中模拟float/double/real?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Bazel(0.26.0)不支持浮点类型,因为您可以阅读

Bazel (0.26.0) does not support float types as you can read here.

但是,我想计算一些魔术(浮点数)数字,将它们存储在 string 中,如下所示:

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

我想使用仅支持Bazel的功能来实现此目的.对我来说很明显,我也可以将计算例如放在批处理/shell脚本中,但是我想坚持使用仅Bazel的功能.任何想法如何实现这一目标?

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?

推荐答案

目前尚不清楚为什么要这么做.整数通常应该足够.

It's unclear why you'd want to do that. Integers should usually be enough.

如果您想要的是序列"0.0 0.005 0.01 0.015 0.02 ... 0.995",则可以这样操作:

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)

这篇关于如何在Bazel中模拟float/double/real?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆