2

Recently I noticed in my current vb.net project that sometimes (depending on the value), if I enter a double value the editor changes my input, for example:

When I type ...

 Dim x As Double = 0.6

...after commiting the line by pressing Enter it becomes:

 Dim x As Double = 0.59999999999999998

If I directly undo the action, then it gets reverted back to the original value I typed in. Any future edit in that line will again produce the undesired results.

I'm aware that internally 0.6 is calculated as 0.59999999999999998 so it doesn't make a difference for the running application, but it's very annoying and the strange thing is: this hasn't always happened and, if I create a new project, it doesn't happen there either.

So my questions are: How can I prevent VS from doing this? Why does it happen at all? Why isn't it always like this (previously in my current project or in a new one)?

I'm running 64bit-Windows 8.1 on an Intel CPU - if that has anything to do with it.

Has anyone got any ideas?

EDIT: I'm using Visual Studio 2013 Professional (12.0.31101.00 Update 4) and, yes, the project has been migrated from several earlier VS versions. There are no relevant Add-Ins or extensions installed. Disabling "Pretty Listing" does indeed prevent the problem, but of course then the other highly useful aspects of the option are disabled as well :o(

EDIT2: After the tip leading to "Pretty Listing" I found the following SO article, but there also no solution was found: Visual Studio VB pretty listing settings

Community
  • 1
  • 1
mike
  • 1,627
  • 1
  • 14
  • 37
  • What version/edition of Visual Studio? Any significant Add-Ins or Extensions? – Damien_The_Unbeliever Jul 15 '15 at 13:31
  • I have never seen such behavior, it might be because of an add-in? You could try appending an "R" to denote a Double literal: `Dim x As Double = 0.6R` – Meta-Knight Jul 15 '15 at 13:34
  • Or even better, use Decimal type if that's possible... – Meta-Knight Jul 15 '15 at 13:35
  • Visual Studio does **NOT** do this by itself. It **must** be an extension of some kind. – Joel Coehoorn Jul 15 '15 at 13:44
  • When a project is acting strange, I close it and use Windows File Explorer to go to the project directory. Then delete the "obj" and "bin" folders. Reopen the project and perform a complete Build of the solution. Its worth a try. – TnTinMn Jul 15 '15 at 15:10
  • 1
    Code running inside Visual Studio has changed the math coprocessor control word. Either changing the calculation precision or the rounding mode, probably the latter. Yes, pretty painful when it happens while you are editing code. Finding the evil-doer is not easy. Beyond add-ins, I know that the Microsoft ACE data provider [does this](http://stackoverflow.com/a/16917514/17034). You could be using it in the Server Explorer window for example. – Hans Passant Jul 15 '15 at 15:14

1 Answers1

0

Several years ago I had a project where this was happening. As you wrote, the reason for the number is that some numbers can't be represented exactly in a binary floating point variable. More on this here.

My project had been upgraded through several versions of Visual Studio. It was also shared with developers using VS Express.

If I ran into this problem again today I would open and check the project-file manually, or just simply create a new project and re-add the files if I could not quickly locate the problem and the project was small enough.

If you have upgraded the project through multiple versions of Visual Studio like I had, it might be dragging some settings along that are no longer visible in the projects options dialog.

Just as an interesting experiment you could also try to turn off Visual Studio's Pretty Listings to see if that is what is actually changing your code.

Stokke
  • 1,871
  • 2
  • 13
  • 18
  • The project is quite large with several dependencies - manually recreating it would be quite a hassle. If i ever get around to it, I'll try to remember to update this post. And, yes: Pretty Listings appears to be part of the issue. Thanks your time! – mike Jul 15 '15 at 15:00
  • No problem. Like I wrote I would then open the .vbproj file in a text editor and look for differences between it and newer project files. In particular any compiler options. – Stokke Jul 15 '15 at 15:21