I'm trying to represent a number such as 1.45E-15 in C... How do i do this... I know there's pow() & exp() function but i don't think they are useful here...

Recommended Answers

All 6 Replies

I'm trying to represent a number such as 1.45E-15 in C... How do i do this... I know there's pow() & exp() function but i don't think they are useful here...

Do you ask of how to convert 1.45E-15 in its binary IEEE 754(1987) floating point representation? For example, if length of exponent is 8 and length of mantissa is 23 bits (1+8+23 = 32 bit poor old float format) 1.45E-15 is 0 01001101 10100001111011110001110 where 0 = sign bit, 01001101 = exponent (effective exponent + bias 127dec), and 10100001111011110001110 = mantissa (without famous leftmost 1 what has usually been dropped, so effective mantissa would be: 1 10100001111011110001110)

Do you really mean this? If you want to make such conversion, you need to know three things: the length of exponent, the length of mantissa, and most important the bias of the exponent, for example for the old float format bias could be 127 or even 128. Do you know these parameters for your own conversion? If so, you can post them, and i would guide you step-by-step how to convert it merely manually:)

You may read this famous paper by Goldberg: http://www.validlab.com/goldberg/paper.pdf

krs,
tesu

What do you mean 'represent' double myVar = 1.45E-15; should be fine.

Noticed! Thanx....

you mean you just want a variable to be "represented" as an "exponential value" that you can then perform some ____ operation upon?

well that's meaningless because theres no such thing, really.

theres essentially only two types of variables: integers and floating points

an integer variable type is just a numeric value ultimately held in machine code by transistor switches, most easily conceived as values of ones and zeros. how you want to "represent" them (binary, octal, decimal, hex, ascii char) is superficial, and meaningful only to the human reading them.

floating points are also just some machine code represented by transistor states (on/off, one/zero), but they have the additional complexity of being divided up into fields for sign, mantissa, and exponent. (exponent offset and binary point are implied.) How you want to "represent" them (standard decimal or scientific exponent) is likewise superficial and only meaningful to the user.

printf and sprintf are the constructs by which you are able to format how variables are "represented". How you choose to do so is your own business and has no effect on the variable itself.


.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.