Single Data Type

1. confused and single Guest

## Single Data Type

Can someone explain to me in English what the range of a single data type is? I don&#039;t understand the E stuff. <BR>

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## RE: Single *Precision* Data Type

It&#039;s not just the range that matters, it&#039;s also the *precision*.<BR><BR>With a single (kibitzers: see CSNG if you don&#039;t know what we are talking about), you only have 4 bytes to store the number. Now, if you use 4 bytes to store an integer, you can store +/- 2 billion... 9 digits and a tad. <BR><BR>But with floating point, some of the bits out of those 32 bits are used to specify a "displacement" of the decimal point. (It&#039;s actually a binary point, since we do binary floating point, but let&#039;s leave that detail for now.)<BR><BR>It&#039;s something like saying "Okay, this number has integer value 123456, but it has a displacement of -3, so it represents the value 123.456!" Or, if it had a displacement of +3, it would represent the number 123456000. See how it works? The "E" notation means "how many places left [if minus] or right [if plus] should we move the decimal point?<BR><BR>Examples:<BR>1.23456 E+05 --&#062;&#062; 123456.0<BR>1.23456 E+08 --&#062;&#062; 123456789.0<BR>1.23456 E+00 --&#062;&#062; 1.23456<BR>1.23456 E-02 --&#062;&#062; 0.0123456<BR><BR>Hokay?<BR><BR>ANYWAY... A single number has a *precision* of *about* SIX decimal digits, but it has a *range* (per the MS docs) from +/- 1.401298E-45 to 3.402823E38. Which means it can represent numbers in the range<BR>0.000000000000000000000000000000000000000 000001401298<BR>to<BR>3402823000000000000000000000 000000000000000.0<BR>(positive or negative).<BR><BR>That DOES NOT MEAN that it can represent even the number 12345678 with **accuracy**! It can&#039;t! Instead, it will remember something like 1.23457 E+07 (it might actually get 7 digits on this one and come back with 1.234568 E+07).<BR><BR>Only INTEGER data types are 100% accurate representations of all the numbers within their range. Floating point numbers are *always* approximations (unless you just happen to use a whole number with few enough digits...but then why not use an integer datat type?).<BR><BR>Is that any clearer?<BR><BR>

3. single and enlightened Guest

## RE: Single *Precision* Data Type

yes, clearer, but now I&#039;m worried what I should do with money? I can&#039;t have approximations when using money! Managers get picky over pennies. Is single okay to use then?

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## *NEVER* use single...

Especially in a database! Especially with money!<BR><BR>Use DOUBLE. Hey, it only costs 4 bytes extra. If you had 10 million fields in a database that you changed from single to double, you only added 40MB to the DB! Whoopee, on today&#039;s hard drives!<BR><BR>Yeah, back in the dark ages where I come from, 4 bytes mattered (I once wrote a BASIC language that used 6 byte floating point for just this reason! enough for money, and we saved 2 whole bytes per number!). But today????<BR><BR>DOUBLE! All the way.<BR><BR>Now, even double is not *perfect*. And if you were writing something for a bank, they might insist that you use "scaled integers" instead. Meaning, essentially, you would simply use 4-byte (or even 8-byte!) integers and represent pennies instead of dollars. Then every time dollars came in or went out, you converted.<BR><BR>But for 99% of the world...double is fine. Especially if you use the VBS FormatCurrency function to then display the numbers.<BR><BR>

5. single and worried Guest

## RE: *NEVER* use single...

but wouldn&#039;t the precision be even worse? I have a product, that needs to be multiplied by other money, added to this that and the other thing. The number may wind up looking like 90.47394. I don&#039;t want any more decimals than the 2 pennys. Using double would make more decimal places.<BR><BR>I know Im creepy over fractions of a penny, but I don&#039;t want to be the one in the bosses office explaining missing money. Did you ever see Office Space and superman 3?

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## So use pennies...

Or always round your results.<BR><BR>myNum = Round( myNum, 2 ) &#039; 2 decimal places<BR><BR>No, the results are still not *exact*, but you&#039;ll be closer.<BR><BR>No, double makes things *MUCH* better!<BR><BR>If, for example, you used Single to represent money, the maximum amount you could represent accurately would be \$9999.99<BR><BR>With double, you should be able to go to \$999999999999.99, at least.<BR><BR>Especially if you Round.<BR><BR>Or use pennies. Either.<BR><BR>Bill<BR><BR>p.s.: Money is *NEVER* multiplied by "other money"!! The result would be meaningless! What is "money squared"??? Money *is* multiplied by interest rates, periods, etc., but never by another monetary value.<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
11,334

## Umm, can I have my stapler please?

That movie rocked!<BR><BR>Well. IF you computed millions of transactions like they were, sure, you&#039;ll have some fractions left over. Accountants usually write this off as "soft cost" if you&#039;re that worried about it.<BR><BR>I highly doubt you&#039;ll run the risk of losing the amount of money those movies were portraying. Just let the customer pay it then.. he won&#039;t notice fractions of a penny. Our US Gas prices should tell you that... what the hell is \$1.799 a gallon? If you pump EXACTLY one gallon (which is nearly impossible due to singles ;)), do you pay \$1.799 or \$1.80? &#060;still ridiculously high -- sorry to our European readers though ;)&#062;<BR><BR>

8. single and content Guest

## Thanks Bill and Desert

i&#039;m a worry wart, thats all. :-)

9. Senior Member
Join Date
Dec 1969
Posts
11,334

## BTW

When I was stationed overseas, pennies were not legal currency on military bases -- they rounded everything up or down to the nearest nickel. With the amount of people at the bases, the BX, Commissary, Shoppette, and every other entity on the base was never in danger of being bankrupt over pennies (**** AAFES).<BR><BR>Quit yer worryin&#039;...

10. <eop> Guest

## What's an AAFES?

.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•