Haha, well I suppose morbidly geeky would fascinate me as well.
Thank you for the response ^_^, I'm wondering though..how/why floats store values like this? I would think an innaccuracy like this wouldn't go un-fixed if it was fixable.
Haha, well I suppose morbidly geeky would fascinate me as well.
Thank you for the response ^_^, I'm wondering though..how/why floats store values like this? I would think an innaccuracy like this wouldn't go un-fixed if it was fixable.
OS: Windows XP Pro CE
IDE: VS .NET 2002
Preferred Language: C++.
It's not an error, it's a way for the computer to handle high precision decimal numbers. Internally the number is stored in such a way that it can be used for either very large numbers (many places to the left of the decimal) or very small numbers (many places to the right of the decimal).
Basically, the machine represents the numbers in exponential format for floats (one bit for the sign, 7-11 bits (depending on float or double) for another part, and the remaining bits for the mantissa) versus using the binary nature of storage for ints.
Whereas ints are represented like such:
00000000 = zero
00000010 = two
00000100 = four
00000101 = five
etc.
A float is stored with a very different method (rather complex and can vary from compiler to compiler) in such a way that while it is not exact, it holds a much broader range. Internally it looks more like
0 0100101 010010100101010010101000
if you were to break up the bits into the different parts. (some compilers store them in different orders, but the result is the same)
Most of this is academic, unless you want to get into compiler design or very advanced tricks and stuff. The main thing to remember is this: == with floats or doubles is most likely to be false, no matter what the values of the numbers, while != is most likely to be true with floats and doubles, no matter what the numbers.
Like any tool, you use them when appropriate and remember how to use them properly. floats and doubles are useful for representing money, temperature, and anything else that might have a fractional part, ints are useful for most other things.
>I'm wondering though..how/why floats store values like this?
Good stuff
My best code is written with the delete key.
Why?for representing money, temperature,
Due to the inaccuracy of floating-point types, isn't it better to simply use a larger integer type and divide by a set amount to get the real answer?
i.e.
Code:float fNumber = 150.25;//normal, but possibly inaccurate long long lNumber = 150250000;//just divide this by 6 and you can get the real value. you can use == on the long longs and just cast them to float and divide by 6 for output
Fixed-point math is indeed popular, but 6 is a weird radix. You usually use powers of 2 or 10.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Thank you Jessycat and Prelude for your help I'm taking a look at that pdf right now infact.
What do you mean by radix?
OS: Windows XP Pro CE
IDE: VS .NET 2002
Preferred Language: C++.
radix == base, from what I remember of the docs on atoi() or itoa() (both nonstandard apparently.. *sigh*) or whatever
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
It was just an example; you can make it whatever you want (as long as it will fit in the variable).
atoi is standard, itoa is not. And yes, radix is the base of the number system. Decimal has radix 10, binary radix 2.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
>>atoi is standard, itoa is not
Oh. Well, good to know at least one of them is usable
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
I use itoa a lot, and I really don't like all the compiler warnings I get.
My solution is just to rewrite it and call it my_itoa
i Just thought i'd take a shot at the initial problem:
i think the (strcmp) thing is right, im new to this too.Code:#include <iostream> using namespace std; int main() { char password[20]; cout<<"Enter Password: "; cin.getline(password, 20); if(strcmp(password, "PASSWORD")==0) // your password in "" { cout<<"Correct!"; .......Whatever you want it to do..... } else{ cout<<"Wrong!"; } return 0; }
As explained before, this is terrible protection because you can view it in a hex editor or disassembler (hell, even notepad!)
Encryption is a very important aspect of computer programming. As prospective computer scientists we should strive to become encryption experts. Go here to learn more.
- "Problem Solving C++, The Object of Programming" -Walter Savitch
- "Data Structures and Other Objects using C++" -Walter Savitch
- "Assembly Language for Intel-Based Computers" -Kip Irvine
- "Programming Windows, 5th edition" -Charles Petzold
- "Visual C++ MFC Programming by Example" -John E. Swanke
- "Network Programming Windows" -Jones/Ohlund
- "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
- "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel
>My solution is just to rewrite it and call it my_itoa
Or you could use sprintf, or better yet, stringstreams. That would save you the trouble of potentially introducing unnecessary bugs by reinventing an existing solution.
My best code is written with the delete key.