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

Numeric comparison: 9 < 10 but 16.9 > 16.10 #6054

Open
AArnott opened this issue Jan 21, 2021 · 6 comments
Open

Numeric comparison: 9 < 10 but 16.9 > 16.10 #6054

AArnott opened this issue Jan 21, 2021 · 6 comments

Comments

@AArnott
Copy link
Contributor

AArnott commented Jan 21, 2021

Issue Description

MSBuild seems to recognize integers when using inequality operators with string operands (which is good), but it fails to recognize floating point numbers as numeric, instead treating them like ordinary strings. This leads to unexpected evaluation of the conditionals.

As shown in the repro steps, MSBuild recognizes that 9 < 10, but fails to recognize that 16.9 < 16.10. It does recognize 4-component versions though, recognizing that 16.9.0.0 < 16.10.0.0.

Steps to Reproduce

  <Target Name="versiontest">
    <Message Importance="high" Text="'9' &lt;= '10' " />
    <Message Importance="high" Condition="'9' &lt;= '10' " Text="true" />

    <Message Importance="high" Text="'16.9' &lt;= '16.10' " />
    <Message Importance="high" Condition="'16.9' &lt;= '16.10' " Text="true" />

    <Message Importance="high" Text="'16.9.0.0' &lt;= '16.10.0.0' " />
    <Message Importance="high" Condition="'16.9.0.0' &lt;= '16.10.0.0' " Text="true" />
  </Target>

Expected Behavior

  '9' <= '10'
  true
  '16.9' <= '16.10'
  true
  '16.9.0.0' <= '16.10.0.0'
  true

Actual Behavior

  '9' <= '10'
  true
  '16.9' <= '16.10'
  '16.9.0.0' <= '16.10.0.0'
  true
@AArnott AArnott added bug needs-triage Have yet to determine what bucket this goes in. labels Jan 21, 2021
@Forgind
Copy link
Member

Forgind commented Jan 27, 2021

But 16.9 is greater than 16.1...

I'm more surprised by the four-part version being successfully recognized. I thought we had special property functions to deal with that, but apparently it sees them as versions anyway.

I'd honestly say this is exactly what I'd expect. If you want to compare versions (and have 16.9 be less than 16.10), you can use the version comparison functions.

@Forgind Forgind removed the needs-triage Have yet to determine what bucket this goes in. label Jan 27, 2021
@AArnott
Copy link
Contributor Author

AArnott commented Jan 27, 2021

you can use the version comparison functions.

What are these "version comparison functions"?
For now I am working around it by artificially adding .0.0 to the end of my floating point numbers.

@Forgind
Copy link
Member

Forgind commented Jan 27, 2021

See #4911
So it would look like:
<Message Importance="high" Condition="$([MSBuild]::VersionLessThanOrEquals('16.9', '16.10'))" Text="true" />

@AArnott
Copy link
Contributor Author

AArnott commented Jun 6, 2022

@rainersigwald Was this fixed or closed as won't fix? The PR that @Forgind linked to above doesn't address this.

@rainersigwald rainersigwald reopened this Jun 6, 2022
@AArnott
Copy link
Contributor Author

AArnott commented Jun 6, 2022

FWIW @Forgind's PR does provide a workaround that's acceptable. This issue just represents the unintuitive experience when you don't know about those property functions where msbuild recognizes integers and a.b.c.d versions automatically but doesn't recognize floating point numbers. So while IMO this would ideally be fixed, I'm not blocked and you can Won't Fix if you want.

@rainersigwald
Copy link
Member

The thing is there's code to do floating point numbers:

protected override bool Compare(double left, double right)
{
return left > right;
}

So if it's not being used that's a problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants