Math problem..
Blitz3D Forums/Blitz3D Beginners Area/Math problem..
| ||
Hey, check this piece:Graphics 640,480,16 SetBuffer BackBuffer() Text 20,20,Ceil(4892280*1212/25125112) Flip While Not KeyHit(1) ;Hit ESC to exit! Wend This piece of math should be 236. But it says, it's 65. What's wrong here?? |
| ||
for some reason you need a decimal point after one of the first numbers. I guess floats can hold larger numbers than ints can. Print Float(4892280*1212)/25125112 Print Float(4892280)*1212/25125112 Print 4892280*1212/Float(25125112) |
| ||
Thank you very much, Coorae!! I have read about the Float command in Command Reference, but I don't understand it completely. My problem has been solved, but I don't like, that I don't understand why it works now.. I gathered that the so called "integer" numbers, cannot be decimal numbers, like 3.6 or 8.2.. If you do that anyway, it will just be 3 and 8, right? In the Float example, it says that 12 / 5 will be 2, because of the integer system. That, I DO NOT understand. Can this be explained by someone, in a somewhat easy-to-understand way? (..if it's not too much trouble..) ;-))) |
| ||
an Integer is a whole number.. a Float is a decimal number Int(2.4) = 2 (Rounded down) Int(2.6) = 3 (Rounded up) |
| ||
All righty then!! I understand now. Thank you! |
| ||
Nice Jim Carrey impression ;) |
| ||
Integers are a class of numbers that only hold "whole" numbers. There are different types of integers possible, from a bit (like a switch, which can be either ON or OFF), a byte (normally 8 bits treated collectively), a word (two bytes treated collectively), a long ingeter (ofen just called a "long", which is four bytes treated collectively. This is how many states a bit can have: 0 or 1 (two) This is how many states a byte can have: 00000000 - represents a count of zero 00000001 - represents a count of 1 00000010 - represents a count of 2 00000011 - represents a count of 3 00000100 - represents a count of 4 00000101 - represents a count of 5 00000110 - represents a count of 6 00000111 - represents a count of 7 00001000 - represents a count of 8 00001001 - represents a count of 9 00001010 - represents a count of 10 00001011 - represents a count of 11 ... - ... 11111110 - represents a count of 254 11111111 - represents a count of 255 So a byte can represent the values between 0 and 255. With a word (two bytes), there would be 0000000000000000 bits in all, and that can count from 0 up to 65,535 (if you don't believe me, try it for yourself). Actually, there is an easy way to figure out how large a value an integer can be, based on the number of bits that it has: 2^n-1, where n represents the number of bits. So for 8 bits (a byte), it would have been 2^8-1, or 255 (which is what our abbreviated table attempted to show). Signed integers, the most common kind of integers, also have to allow for some way to show that a number is either positive or negative. That is a two-state condition, which could be represented by a single bit. By convention, the uppermost bit of an integer may be reserved as a sign bit. When zero, the number is positive, when a 1, the number is negative. Since we lose one bit to represent the sign bit, the largest value a signed 16-bit word can have is 2^(n-1)-1, or 32,767. Note that integers do not have a means of expressing fractions or larger values than allowed by the number of bits that they include. For fractions, you can either use another class of numbers called Fixed point, where a decimal point is assigned a fixed position within the numeric representation, or you can use the Floating Point method ("Float" for short). Floats also have three other advantages: (1) They floating decimal format can be used to represent extremely large and extremely small numbers, (2) The floating point unit (FPU) in modern PCs makes the time to process Floats a lot faster and less of a time penality than they use to me, and (3) the use of FPUs has also standardized the method of representing Floats between programming languages. What Blitz has done is make the decision that so many different ways of representing numbers is counterproductive. You have to support an integer type, because that is what calculates the fastest, so it uses the 32-bit integer form, which is the long integer form, which can hold a sign value of up to 2,147,483,647 (2^(32-1)-1). That is over 2 thousand million (a gig)! If you want any more than that, or if you want to have decimal representation, you need to use the Float form instead. Of course, indirectly, Blitz also recognizes the byte form, as the character byte is what we use to represent keyboard and string data with, so you have the Asc() command to get the byte value (ASCII code) out of a string, and a Chr$() command to put a character byte value back into string form. We have integer arithmetic capabilities in computers because we built the computers using binary (two-state bit) structures that are grouped into bytes, words, and double words, and we provided some commands to treat these as unsighed integers and others to treat them as signed integers. That makes them fast at doing integer arithmetic, and that is why game programmers try to use integer arithmetic wherever possible -- for speed. They came up with hardware and software solutions to handling fixed point values, because people wanted to use computers to process money, and to make sure the counts were accurate to the penny, they had to avoid round off errors. And for the engineers and scientists, they came up with Floats, which are a similar to Scientific Notation, only it is a binary representation involving powers of two, rather than being based on the decimal system and its powers of ten. If we could do the things with integers that can be done with fixed point and floats, then we would do everything with integers, but we can't. But as computers get ever faster, programmers are learning that sometimes the results are worth the extra computational power required by using floats, so that is becoming more common. And with computers on the verge of gaining 64-bit words as part of their architecture, integers will be able to represent ever larger values, although never as much as Floats can, or as small, or to the same degree of precision (since they do not support decimal places in native mode). |