C# Workshop - Week 2 (Ch. 5 - 6)

Started by
22 comments, last by SpiderPig 16 years, 7 months ago
ah I see, ok thanks alot :)
Advertisement
Hi, once again thank you so much for taking time to answer my questions since this course has ended and all that.

Anyway, I have always been puzzled as to the difference between a decimal data type and a float date type. Am I right to say that decimal is more accurate but uses more memory then the float? But since in todays world our storage capabilities are enormous so the difference in memory used is negligible? So its just basically about being technically right where if we dont need a decimal we just use the float but if I'm lazy I can also just use decimals for everything
Spider, using decimal instead of float suffers three problems.

1. Decimal is 16 bytes, while float is only 4. There is an obvious 4x increase in the amount of space required. This doesn't seem like much, but consider using a decimal instead of a float in something such as a terrain mesh.

Lets assume you've got a high-res 512x512 meter map, with 2 to 4 verts per meter. At a 2 vert per meter resolution that means you're looking at 1025x1025 vertices. Multiplying that out you get 1,050,625 vertices. That's a lot of vertices. Each vertex has a x, y, and z coordinate (in addition to color and UV frequently, but we'll ignore those for now). Just using x, y, z we're looking at 48 bytes per vertex. Multiply 48 bytes by 1,050,625 and you get 50,430,000. That's 50 MB of memory, just to store a terrain! That same terrain would only be 12 MB using floating point values. Now imagine trying to have moving models on the screen, all using decimal for vertex positions. You would very quickly run out of memory or at the very least destroy your cache.

2. Most ALU's on modern processors are optimized to work with integer and floating point data types. This usually means int, bool, float, and probably double. However, Decimal is a relatively new type, and as a result it's unlikely there is strong hardware support for it yet. Note, I'm only speculating, but I'm guessing that not only does Decimal have a space penalty, but it probably also has a performance penalty as well.

3. While it's true that the Decimal data type has a higher precision, it has a lower range in values. A floating point value can go up to 3.4e38 (with 7 digit precision). The decimal type can only go up to 7.9e28 (with 28-29 digit precision). This means that if you've got, or could potentially have a number larger than 7.9e28, you will have no choice but to use a float or double.

As with all things in programming and in life, there's a specific time and a specific place to use all tools and techniques. Not using the appropriate tool for the appropriate task always comes with a price. Sure, you could use a hammer when a screwdriver is the correct tool, but chances are you're going to damage something in the process.

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
ah alright i have a clearer picture of the differences now, thanks alot. I use to be in this programming class that was using VB 2005 and I asked what a float was but was given then "we dont use that" aka "u dont need to know" answer by the teacher, but digress. Thanks for answering

This topic is closed to new replies.

Advertisement