Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable interpolation doesn't work for floats #7887

Closed
joshuaspence opened this issue Jul 31, 2016 · 11 comments · Fixed by #8590
Closed

Variable interpolation doesn't work for floats #7887

joshuaspence opened this issue Jul 31, 2016 · 11 comments · Fixed by #8590
Assignees

Comments

@joshuaspence
Copy link
Contributor

I'm testing Terraform at HEAD and it seems that variable interpolation inside a string assumes that arguments are integers. For example, the following snippet fails:

data "template_file" "test" {
  template = "${x + y}"

  vars {
    x = 0.001
    y = 0.002
  }
}

The error message is:

Error applying plan:

1 error(s) occurred:

* data.template_file.test: failed to render : __builtin_StringToInt: strconv.ParseInt: parsing "0.001": invalid syntax

Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.

It looks like x and y are being parsed as integers rather than floats because ${0.001 + 0.002} works fine.

@kwilczynski
Copy link
Contributor

@joshuaspence I am curious, what is your use case?

@phinze
Copy link
Contributor

phinze commented Aug 1, 2016

Hi @joshuaspence - thanks for the report. I seem to recall this being an issue for a little bit now, but I can't find the duplicate right now. I'll link back if it comes up.

You should be able to work around this bug by forcing float math by adding 0.0 + to the front of your expression. Hope that helps!

@joshuaspence
Copy link
Contributor Author

The use case is that I'm building a Grafana dashboard and I want to set a threshold on a graph to be just-a-little-bit-smaller-than an integer value. I was trying to do this as ${number_of_instances - epsilon}.

@joshuaspence
Copy link
Contributor Author

Thanks for the workaround @phinze.

@kwilczynski
Copy link
Contributor

@joshuaspence ah, got it. Thank you!

@phinze
Copy link
Contributor

phinze commented Sep 2, 2016

Turns out we need to rethink this fix - reopening!

@phinze phinze reopened this Sep 2, 2016
@mengesb
Copy link
Contributor

mengesb commented Dec 16, 2016

So I have a use case here surrounding cidr math.

> "${(5/2.0)}"
2
> "${(5.0/2)}"
2.5
> "${(5/2)}"
2

The above shows that if the numerator is a float, then floating point math is initialized. I would presume that it should always be floating point math, and we could use the ceil() or floor() functions to return integers as a result of unwanted floating point returns.

My use case is computing subnet space within a given cidr. For example we have 5 total subnets, I need to compute the largest subnet within a given subnet (for example, 10.20.30.0/24).

Needing 5 subnets in a /24 subnet means the largest subnet size I can use is /27 - ${cidrsubnet("10.20.30.0/24", ceil(5/2), count.index)} SHOULD return subnets:

  • 10.20.30.0/27
  • 10.20.30.32/27
  • 10.20.30.64/27
  • 10.20.30.96/27
  • 10.20.30.128/27

However; ${ceil(5/2)} currently returns 2, so the bits are /26 in the math, and I fall over the subnet limitation for the size.

Since I am breaking up that 5 into "2 + 3" and instead of forcing the user to input "2.0 + 3.0" I'm not getting floating point math. I think it is reasonable to return a float in ALL situations of division

@mitchellh
Copy link
Contributor

Hey @mengesb there is a new issue that was just opened today actually and we've committed to this behavior. I'm going to close this but would you mind watching that issue (should be easy to find with label:bug label:core since it was today).

@mengesb
Copy link
Contributor

mengesb commented Dec 16, 2016

Seems that math in general has some issues:

> "${2 + 3.1}"
5
> "${2.1 + 3}"
5.1

@mengesb
Copy link
Contributor

mengesb commented Dec 16, 2016

Reference: #10778

@ghost
Copy link

ghost commented Apr 2, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants