Widening conversions and option Strict on

Asked

Viewed 56 times

0

I’m learning about conversions and reading that article:

Widening Conversions (I don’t know the term in English)

  The following conversions may lose precision: 

       Integer to Single

       Long to Single or Double

       Decimal to Single or Double

  However, these conversions do not lose information or magnitude.


 "As seguintes conversões podem perder precisão:

      Inteiro para Single

      Long para Single ou Double

      Decimal para Single ou Double

  Contudo, essas conversões não causam perda de informação ou magnitude"

I don’t understand what that means. If I for example had an integer of value 2 and tried to convert to single, how this loss of precision would happen?

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

2

I’ll get the third conversion of your question as an example.

  • A decimal variable has the accuracy of 28 or 29 digits.

  • A double variable has the precision of 15 or 16 digits.

Decimal variables are highly recommended when dealing with data involving finance.

Example: You own a company that has a birthday party. You use a decimal variable to store the dollar price of a lollipop you want to buy to put in the parties that your company organizes. You for some reason decide to convert this value from the lollipop unit to a double type variable.

In the example you decide to buy 10000000 (ten million) lollipops, separating EXACTLY the amount of the cash amount for the purchase, based on multiplying the value of the variable double times 10000000 (ten million).

With this conversion surely will be missing or left money (if there is no approximation of the last decimal place), because the accuracy of the value was lost in the value you found, but at the time of purchase the seller will use the most accurate value (decimal).

Even more so if for some reason you make this purchase in another currency. The conversion to the real would already have a difference.

In the example you mentioned, having an integer variable of value 2 and converting it to a single, would have no problem, because this is a value whose accuracy is tiny, which both an integer and a single variable encompass.

On the loss of magnitude or information, see an example where you have 3 variables (A, B and C):

  • A = decimal type variable
  • B = double-type variable receiving A
  • C = decimal type variable receiving value of B

"In code":

A = valor qualquer
B = A
C = B

C would have exactly the same value as A, even if a conversion to B was made.

Links that helped me answer your question:

http://www.macoratti.net/12/12/c_num1.htm

https://social.msdn.microsoft.com/Forums/pt-BR/f91fc4dd-47f3-4596-a94f-9c93d8bddc99/capacidade-de-variveis?forum=vsvbasicpt

https://www.caelum.com.br/apostila-csharp-orientacao-objects/variables-primitive/#variable operations

http://excelevba.com.br/variaveis-no-vba/

2

This example would have no loss, this conversion is the call widening, then he’s taking something narrow and making it wider. You take something that exists in the other kind.

Accuracy is detail, the more accurate the closer to the number you need. Accuracy is not accuracy, this feature requires that the number is exact what it needs.

Loss of information or magnitude is you have a value 1000 for example and for some reason it turn 100, or -200 turn 200.

The integer allows numbers between -2,147,483,648 and 2,147,483,647. This is because all these numbers fit into slots from 2 to 32, so we have just over 4 billion slots. Where does this number come from? 2 is the possible values of the binary numbering system, and 32 is the amount of bits an integer has (4 bytes).

A Single has also 32 bits, so you can imagine that because it has a decimal part it cannot have all these more than 4 billion integers. Don’t get me wrong, he can go beyond these limits of 2 billion positive and negative, but he can’t represent all the possible integers. It skips a few. It does this because it doesn’t depend on being precise, you can work with an approximate number and that’s okay. It has a complicated way for most people to understand what the possible numbers are, and what they are depends on some factors of the number, only occurs in very large numbers.

In some cases it may be that you can’t represent number 3, but you would get 3.0000000000002 (just one example, I’m not getting a real number that would happen this). That number is almost 3, but not 3. That’s why cannot use this type for monetary values. In certain problems almost 3 is good and serves the purpose. Note that in small numbers there will never be loss, all integers can be represented. Only with very large numbers would he have to use tricks that would not represent the exact number.

The Double has 64 bits so it can represent all over 4 billion possible integers, plus a lot of others, and can also represent decimal part. That’s why he never loses that kind when he converts from Integer.

But a Long for Double can have loss because this type also has 64 bits and can behave much more integers, so of course another type that can have whole part and decimal part of the same size in memory does not have to represent everything.

The Decimal has 128 bits, so it’s even worse.

  • Could you tell me a situation where a conversion from Integer to Single would lose precision?

  • As I put in the answer in very large number.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.