Thread: mantissa, sign, exponent

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    mantissa, sign, exponent

    so C is a portable assembly language.. if I am asked to create a program to extract the sign mantissa and exponent on a 32 bit machine and 64 bit machine.. can I just use the same exact code?? as far as I know the location of the exponent, mantissa, and sign bit is totally different in the 64 bit version so therefore the way we mask it is also different. So, because of this we can't use the same exact code, inside the code we should check whether it is a 32 bit machine or 64 bit machine first and then use the corresponding mask to extract the sign, etc.. Please correct me if I am wrong

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, no, not exactly.
    The floating point uses the IEEE standard for it I think, so all machines that uses this standard handles floating points exactly the same way.
    x86 and x86-64 should both use the IEEE standard.
    What you need to look out for is whether it's float or double, because they use different amount of bits to store different types of the data.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can use the exact same code if the new machine stores floats the exact same way.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    yes it's just float.. and what do you mean the machine stores float the same way?? on the 64 bit and 32 bit machine they store it differently according to the IEEE

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I doubt that, though I haven't looked into the docs about that. So long as the size is the same, I doubt the layout will be any different.
    Otherwise you could point out where you found out this is the case.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so even if it's in a 32 bit machine or a 64 bit machine.. it will still work?? because I read on wikipedia about the IEEE and they mentioned the layout for floating point number is different in 64 bits:
    the mantissa is at bits 0-51 and the exponent is from 52-63 and the sign bit is 64. I read it here:

    http://en.wikipedia.org/wiki/IEEE_fl...point_standard

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    From that same page:
    IEEE 754 specifies four formats for representing floating-point values: single-precision (32-bit), double-precision (64-bit), [...]. For example, the C programming language, which pre-dated IEEE 754, now allows but does not require IEEE arithmetic (the C float typically is used for IEEE single-precision and double uses IEEE double-precision).
    In other words, a "float" in C is usually single-precision, or 32-bits; and a "double" is usually double-precision, or 64 bits. You can see this by using sizeof(float) and sizeof(double); those values are usually 4 (8 bits * 4 bytes = 32 bits) and 8 (8 bits * 8 bytes = 64 bits). This should be the same on all platforms. You can check by using sizeof(float)/sizeof(double) on both platforms. If they're the same value, it's likely that the format is the same.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    oh I see how this works, so in C a float is always stored in a single precision and it doesn't matter if it's a 32 bit machine or 64 bit machine?? so all these days I was always wrong, I was thinking that all 64 bit machines ALWAYS represents floats in 32 bit machine.. this is wrong right??

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Floats are 32-bit, so of course they're 32-bits on 64-bit machines, as well.
    So long as the compiler adheres to the IEEE standard, the code will be correct wherever you might compile it. And as long as the underlying architecture adheres to the IEEE standard, the code will execute right wherever you run it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    oh I see.. how stupid am I to think these days that they are different on different machines.. can you name some examples of machines that uses a double precision to store a float.?

    so even a 32 bit machine can use a double precision floats??

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I have no idea of such a machine and I doubt there would exist one, from my inexperienced knowledge of course.
    x86 and x86-64 uses single for float, so 90% of the computers out there will have no trouble whatsoever.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I see..that makes more sense.. I just got this weird question tingling inside of me.. my instructor wants us to use appropriate typedefs for the int_4, int_u8 etc data types.

    These will take different types(long vs long long for eg.) depending on whether the
    machine is 32 bit/64 bit.

    I don't quite get that.. in a 64 bit machine a long is a long long??

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    These types are just different sizes. The types may be different size depending on what platform and operating system it's running under.
    A long is 4 bytes under Windows 64-bit, but 8 bytes under Linux 64-bit IIRC.
    (On 32-bit, it's usually just 4 bytes, regardless of OS.)
    Long long is a compiler extension that creates a 64-bit value (32-bit on Visual Studio).

    You should use appropriate types depending on what system you're targeting. That's why the (u)intxx_t types are so good, because if you use them, you will guarantee to get yourself a type of the right size, wherever your code is compiled and executed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    and so what does this type has to do with extracting the sign mantissa and exponent?? say int_u8, what do I use that for??

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's a type! Just like int, long, float and double.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. floating point number comparison
    By stanlvw in forum C++ Programming
    Replies: 9
    Last Post: 04-27-2009, 01:44 PM
  2. how to convert decimal to floating point number
    By -EquinoX- in forum C Programming
    Replies: 98
    Last Post: 03-04-2008, 01:25 PM
  3. Sign ' is the same as \' ?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 11-23-2007, 07:32 AM
  4. Floating-Point Numbers
    By MindLess in forum C Programming
    Replies: 4
    Last Post: 06-24-2007, 11:45 PM
  5. My own itoa()
    By maxorator in forum C++ Programming
    Replies: 18
    Last Post: 10-15-2006, 11:49 AM