3

This is, at least for me, most bizzare Visual Studio 2010 behaviour ever. I'm working on MVC3 project, I copied a line of code from another project (VS2010 also, MVC1 if it matters) which looks like this:

target_height = height * 1.1

when I paste it into MVC3 project, it gets expanded to

target_height = height * 1.1000000000000001

Now, if I type 1.2, it's fine, nothing happens, but if I type 1.12 it is expanded to 1.1200000000000001.

Both target_height and height are integers. Why does one Visual Studio display 1.1 while other expands it to 1.1000000000000001?

What is going on???

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
Vnuk
  • 2,673
  • 1
  • 32
  • 49
  • look here http://www.wiffin.com/ they also have «read more» link for more info about topic (yes there are no C languages in the list but the idea the same) – Igor Milla Mar 01 '11 at 10:40
  • are you saying that CLIPBOARD doesn't hold text but some other data and that data is interpreted on PASTE? – Daniel Mošmondor Mar 01 '11 at 10:58
  • 1
    No, Visual Studio interprets/replaces it on paste and on me typing it. – Vnuk Mar 01 '11 at 11:00
  • 1
    Can't reproduce. Are you running ReSharper, CodeRush or similar? Are you these C# files or pasting into a Razor view, or something else that might be a factor? – Rup Mar 01 '11 at 11:14
  • 2
    I also cannot reproduce outside this MVC3 project. No ReSharper, CodeRush or other add-ins. These are VB.net controller classes, so Razor is excluded as a factor. If I create new MVC3 project VS does not display this behaviour. Perhaps worth mentioning - this was MVC2 project which I upgraded to MVC3 following upgrade instructions from MVC3 RTM release notes. – Vnuk Mar 01 '11 at 11:35
  • @Suma I think you're right, post your comment as answer, I'll accept it. – Vnuk Mar 04 '11 at 07:39

3 Answers3

1

I think it is autocomplete went crazy and started fixing floating points constant into "allowed" values. As written in http://accessmvp.com/Strive4Peace/VBA/VBA_L1_02_Crystal.pdf , VB autocomplete really tries to offer only "things that apply specifically to that data type". int * double is understandably not truncated into int * int (automatic conversions always happen only as needed) and what you see is double representation of 1.1 or 1.12 (epsilon = 1.11e-16).

I think it would still need some further checking or verification to learn exact conditions when this happens, but as I am not using VB.NET or MVCx this is not something I am willing to do.

Vnuk
  • 2,673
  • 1
  • 32
  • 49
Suma
  • 33,181
  • 16
  • 123
  • 191
0

The numerical literal 1.1 does not actually represent the quantity 11/10, but instead represents the quantity round[(2^53*11)/10]/(2^53), which is a tiny bit larger than 11/10. Although that value could be written out precisely as a decimal number with 53 significant figures, doing so would be about as useful as using an inch-denominated measuring tape to determine that something is 1 3/16" long and recording the measurement as 30.1625mm. If one wouldn't be able to distinguish a measurement that was longer or shorter or shorter by less than 1/64", the measurement would be 30.1625mm +/- 0.396875mm, which is functionally the same as 30.2mm +/- 0.4mm.

The fact that Visual Studio would choose to represent the numeric quantity closest to 1.1 as 1.1000000000000001 is curious. On the one hand, the literal 1.1 would be a more concise representation of the same value. On the other hand, even if the aforementioned literal would be indistinguishable from 1.1, the more verbose representation is not without advantage. In some cases, it may be helpful to know whether a quantity is slightly larger or slightly smaller than what it "appears" to be. Even though the difference between the numeric literal 1.1 and the mathematical value 11/10 is numerically insignificant (multiplying the numeric literal by ten yields precisely 11), the difference between (1.1-1.0) and (1/10) is noticeable (multiplying the numeric expression by 10 yields a value greater than one).

supercat
  • 77,689
  • 9
  • 166
  • 211
  • Really didn't understand anything – Vijay S B Mar 10 '17 at 12:22
  • 1
    @VijaySB: The way computers store numbers can't precisely represent most decimal fractions; an attempt to store 1.1 will instead store the closest number the computer can represent, which is a tiny bit larger, and is actually closer to 1.1000000000000001 than to 1.1000000000000000. The effect is somewhat like representing a fraction like 1/3 as a decimal fraction 0.3. If one were to then try to represent that as a fraction in 27ths, it would be 8/27 (even though the original was 9/27). – supercat Mar 10 '17 at 14:09
-2

1.1 and 1.12 must not have an exact binary representation.

see this : https://stackoverflow.com/questions/634206/what-every-programmer-should-know-about

Community
  • 1
  • 1
Max
  • 3,128
  • 1
  • 24
  • 24
  • Sure - the question is why does VB.NET parse in the number as a float and then re-emit it when copy and pasting the code, as opposed to just replicating the characters copied and pasted. – Rup Mar 01 '11 at 17:05
  • 1
    It is not pasting itself, it is autocomplete (or some autocorrection), which acts after pasting or typing - see also "... if I type 1.12 it is expanded..." – Suma Mar 01 '11 at 19:23