C++

Discussion in 'Scripting & Programming' started by Mof, Jul 10, 2008.

  1. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    If they really wanted to save space they would have done:

    Code:
    float numbers[] = {1.5, 2.5, 3.25};
    
    which is also equivalent. :)
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  2. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    Although if you really wanted to save space you could write it as:
    Code:
    float numbers[3] = {1.5, 2.25, 3.25};
    
    Harry.

    Edit: SNAP! <giggle>
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  3. Mof

    Mof Megabyte Poster

    526
    2
    49
    Thanks for clearing that up for me:eek:

    why have one of you got [3] and the other[]?
     
    WIP: C++ and A+
  4. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    notice that I put '[]' instead of '[3]'? That's because the compiler is able to deduce the size of the array from the data provided, as in sizeof(numbers) / sizeof(float) (which will equal 3 float elements).

    But still 'snap'! :biggrin
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  5. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    As another word on the matter (that I had no time to elaborate on earlier and forgot about), the way that the values are declared actually forces an implicit cast to float from 'double'. To fix this, and what the book should have mentioned, is to append each value with an 'f' indicating that it is a float literal:

    Code:
    float numbers[] = {1.5f, 2.5f, 3.25f};
    
    Now it's well formed! :biggrin
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  6. Mof

    Mof Megabyte Poster

    526
    2
    49

    All the book says at this stage is that float is to 6 decimal places and double is to 10 decimal places.
     
    WIP: C++ and A+
  7. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    See here :-

    http://msdn.microsoft.com/en-us/library/c70dax92(VS.80).aspx
    http://msdn.microsoft.com/en-us/library/tfh6f0w2(VS.80).aspx

    Basically you are defining a literal value when you type a floating point number in the source, these literals can have a type of float, double or long double. 'Implicilty' or 'by default' these values are doubles, there will therefore be some type coercion or an 'implicit cast' to float. Optimising compilers might eradicate this for you but its better to learn to be specific in the first place ! :biggrin

    C++ is a statically typed language, it also has a lot of syntactic sugar, it requires a different mindset to weakly typed or dynamic languages, you really need to learn to think like the compiler. Its quite like assembler in this manner as you need to understand the processor to write assembler.
     
  8. Mof

    Mof Megabyte Poster

    526
    2
    49
    its slowley dropping into place :unsurethanks for the link, Quiet at work today so will practice here, quietly in a corner:tune
     
    WIP: C++ and A+
  9. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    I can see where the author is coming from, but it's the weakest reason for the differences between floats and doubles. The reason why they said this is because floats are smaller than doubles. run the following code to see what I mean:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	cout << "Size of float in 'bytes' is: " << sizeof(float) << endl;
    	cout << "Size of double in 'bytes' is: " << sizeof(double) << endl;
    
    	return 0;
    }
    
    You should see that a 'double' is double the size of a float ('double word' vs. 'quad word' size).

    Another difference is that the reason why 'floats' are called 'floats' is because they actually make use of floating point arithmetic. Recall from my previous post that I suggested an 'f' suffix to prevent an implicit cast to double? I could have just as eqaully insisted that the notation should be:

    Code:
    float numbers[] = {1.5e1, 2.5e1, 3.25e1};
    
    Making use of the propular form in mathermatics of Exponentiation to the base 10. I'd be happy to expand on this if you don't quite get it later. :)

    Now variables of type 'double' make use of 'double-precision arithmetic'. From the code above you saw that the size of the 'double' was twice that of the 'float'? This is because a variable of type 'double' is in fact making use of two adjacent memory locations that are the same as two variable of type float sitting next to each other in memory - literally. So, floats use 'single-precision arithmetic' and doubles use 'double-precision'. So how does this affect the number of decimal places that can be represented? I could get into a lengthy math explanation, but feel that a little more understanding of the theory of binary arithmetic may be required, but if you're comfy with that I'd also be happy to explain in detail tonight. Take it on board for now that depending in the values that can be represented by a variable of a certain type, the larger it is the greater range of values that cfan be represented. Also, if you can viasualise this, because of the space available, a greater range of magnitudes can also be represented - which means that you could obtain greater accuracies for values between 0 and 1 exclusive, as provided by the greater number of decimal places.
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  10. Mof

    Mof Megabyte Poster

    526
    2
    49
    Hi Mathmatix will run the code when i get home later, no compiler on work computers and I dont think they would let me down load one. At the moment I am practicing writing code in note pad. So when I get home later I can re-write and then run the program, I find its A good way of making the information stick in my head, I will be online later and will also check in at CF.
     
    WIP: C++ and A+
  11. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Great stuff, Mof.

    I made a mistake with:

    Code:
    float numbers[] = {1.5e1, 2.5e1, 3.25e1};
    
    It's to do with the values stored. Print the values of 'numbers' and correct them using the same notation. :biggrin
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  12. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    There are different types of number representations in computers :-

    Integer (unsigned) / Integer (signed) or Twos complement

    Fixed point

    Floating point (Similar to our scientific notation but in base two.)

    Floating point numbers are called floating point because the 'point' ie '.' 'floats' and an exponent is used to allow this and represent the general magnitude of the number. The physical representation is packed to save space and defined in an IEEE defined standard. But basically you have an exponent and a mantissa. The mantissa is what determines the precision. Single floating point numbers are 32 bits, double floats are 64 bit, they have double the size and effectively have approx double the precision due to the size of the mantissa (single 23 bit mantissa, double 52 bit). The size of the exponents does not increase by double because it is 2 pow N so this goes up exponentially with increasing numbers of bits, also its base 2 powers not base ten so you would need to use the law of logs to do the conversion to our base ten standard scientific notation.

    One last thing :-

    1.5e1 = 15 not 1.5 and would also still be of type double.

    This is probably what you meant :-

    1 x 10 pow -2 = 0.01

    1 x 10 pow -1 = 0.1

    1 x 10 pow 0 = 1

    1 x 10 pow 1 = 10

    1 x 10 pow 2 = 100

    ...etc

    Its a progression or sequence.

    Just try it on windows calculator...
     
  13. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    This was supposed to be an exercise, dmarsh! As well as being fundamentally incorrect. :dry
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  14. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    Why is it fundamentally incorrect ?

    I have written a software based IEEE single precision floating point library in assembler before as an exercise, before maths co-processors were common, this was however 14+ years ago !

    I think an understanding of the fundamentals is useful for those that are interested, those that aren't interested in computer fundamentals can just skip...
     
  15. Mof

    Mof Megabyte Poster

    526
    2
    49
    1 pow 3 = 1000
    1 pow 4 = 10000
    1 pow -4 = 0.0001
     
    WIP: C++ and A+
  16. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    Exactly in base 10 the exponent is the power of 10 to times the number by or in plain english for a positive exponent and a whole number the number of zeros to add to the right.
     
  17. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    1. Your understanding of two's complement is wrong.

    2. I have never, ever seen this in my entire life of C/C++ either professionally or non-professionally:

    Code:
    float numbers[] = {1.5e0f, 2.5e0f, 3.25e0f}; 
    
    And the way it was originally is of type 'float'. You have assumed a cast back to double which doesn't happen.

    3. Fixed-point arithmetic is ususally represented in code using hex notation!

    4. I didn't say that the IEEE standard is wrong, I said that your explanation was wrong.

    About the standard math bit regarding exponents...

    Yes, that was perfectly correct. I was hoping that Mof could learn from the exercise.
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  18. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    I have to do it professionally, not as part of an exercise, and explain to coders far superior to both you and me put together as to why I have done things a certain way!

    And this is the reason why people find C++ hard, because it is a language that deals very heavily with the 'fundamentals' that people don't understand. What is the consequence? The write code and it doesn't behave as expected because what they have written actually has undefined behaviour.

    When I was learning I paid particular attention to all details regarding the underlying structure of both hardware and software - not necessarily by choice (as my degree was very demanding and they made you do it). I'm glad that I did and would strongly recommend anybody learning C++ to pay attention, or be a weak C++ programmer.

    Simple as that really. :rolleyes:
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  19. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    I did not describe twos complement, I merely pointed out it is a way to represent a signed integer value used in computers.

    Hey its your code buddy ! I was correcting your suggestion ! There is no need to times by x pow 0 because x pow 0 always equals one ! Thats why you would never see it in code, its basic number theory.

    You originally said it was of type double and there is a cast, so which is it ?

    This is the microsoft website, in general floating point literals are double, which would then mean an implicit cast to float, maybe the compilers smart enough to figure out it requires a float in an array definition, I doubt it, but its been a while since I coded in C++ so I could be wrong on this...

    Not necessarilly when I talk about representation I would be talking about the computers representation not what one might see on a screen. Its really a stepping stone to more complicated number storage systems, its common to use these sort of things when you require a high 'fixed precision' in a limited range for things like monetary values.

    What ? What did I say that is incorrect? What are you on about ? (Ok I missed of the 10 pow x when i expained exponents ! a basic typo) But specifically what in my current explanation is wrong ?

    So you made a deliberate mistake ?

    Eh ? :rolleyes: Whats that mean ? I've coded professionally in many languages for 14+ years... My point is nothing I've said is advanced math or unsuitable for a Computer Science student.
     
  20. BosonMichael
    Honorary Member Highly Decorated Member Award 500 Likes Award

    BosonMichael Yottabyte Poster

    19,183
    500
    414
    Coder fight! CODER FIGHT!!!

    Two men enter; one man leaves. :popcorn
     
    Certifications: CISSP, MCSE+I, MCSE: Security, MCSE: Messaging, MCDST, MCDBA, MCTS, OCP, CCNP, CCDP, CCNA Security, CCNA Voice, CNE, SCSA, Security+, Linux+, Server+, Network+, A+
    WIP: Just about everything!

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.