Closed Thread Icon

Preserved Topic: Extracting the decimal (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=17357" title="Pages that link to Preserved Topic: Extracting the decimal (Page 1 of 1)" rel="nofollow" >Preserved Topic: Extracting the decimal <span class="small">(Page 1 of 1)</span>\

 
Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 12-31-2002 18:35

ok, programming in C- say I have this:

float mynumber = 3.546;

I want to extract only the numbers after the decimal point (eg. '546') and not what came before (eg. '3')..

(in theory, I want to see if the number is an integer or if it has decimals)

Also, in another problem on the same script, I want to see how I can make C give me only- say- up to the first decimal (eg. '3.5'), but I don't know how to do it..

I thought I could find help here, but I guess not...

anybody got an idea?

edit- wow.. interesting side-note- seems you can find great help on all the standard C libs here...


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

[This message has been edited by Petskull (edited 12-31-2002).]

Xdreamer.ch
Maniac (V) Inmate

From: Switzerland
Insane since: Mar 2001

posted posted 12-31-2002 19:08

hmm...sounds like you need something
like an "round()" thing...sorry can't
give you more hints because I'm ***x
in coding...no logical half of my brain
want to let me do something like that

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 12-31-2002 19:45

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

lovedove
Bipolar (III) Inmate

From: Orlando, FL, USA
Insane since: Dec 2002

posted posted 12-31-2002 21:23

This is probably not very efficient, but can't you do something like this:

float n = mynumber;

if(n>0)
while(n>1)
n--;
else
while(n<-1)
n++;

and when it ends n will be the decimal?

edit: erk, it messed up my spacing.. but i guess you can tell what i meant


[This message has been edited by lovedove (edited 12-31-2002).]

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 12-31-2002 23:16

check the modf (for doubles) and modff (same thing for floats) functions. Just to let you know the core theory here is using "modulus" which is generally gives you the remainder of something.

Thoretically if you take your num (fn)and create another variable equal to it then typecast the second number as an integer, then typecast it back to a float if the two don't match the first number wasnt an integer

(I'm not a C guy but here's pseudo code)

n = 3.44
x = 3.44
int x (x now equals 3)
float x (now 3.0)
if (x != n) then n was not an integer

Just a hack that probably would have worked



.:[ Never resist a perfect moment ]:.

[This message has been edited by bitdamaged (edited 12-31-2002).]

silence
Maniac (V) Inmate

From: soon to be "the land down under"
Insane since: Jan 2001

posted posted 01-01-2003 00:49

Yeah, good advice all around. Here's how I'd do it.

code:
float mynumber = 3.546;
int x = int(mynumber);
float return_value;

return_value = mynumber - x;



See, even if mynumber is negative, that means x is negative as well, and (-mynumber) - (-x) = (-mynumber) + x. Therefore, you'll get the part you want and positive and negative won't matter.

To clarify, here's a brief run through:

mynumber = 3.539
x = 3
return_value = 3.539 - 3 = 0.539

mynumber = -3.234
x = -3
return_value = -3.234 - (-3) = -0.234






[This message has been edited by silence (edited 01-01-2003).]

silence
Maniac (V) Inmate

From: soon to be "the land down under"
Insane since: Jan 2001

posted posted 01-01-2003 01:19

You know, looking at it again, this seems a bit inelegant. I had thought there would be a built in function for this purpose, but after looking through math.h and float.h I wasn't able to find one.

Therefore, I'd recommend just writing a function to do the job.

Here's the function prototype:

float func_name(float input_float, int decimal_places);

Now, once you have the decimal part of the number, for example 0.123 (from 3.123), you can then drop as many decimal places as you want. For example, here's a sample algorithm to return the number using only one decimal place:

1) Take remainder (0.123)
2) 0.123 * 10 (for the first decimal place) = 1.23
3) temp_int = int(1.23) = 1
3a) temp_float = temp_int / 10
4) int(input_value) (in this case int(3.123 = 3))
5) return (input_value + temp_float)

Now, if you wanted 2 decimal places:
1) take remainder (0.123)
2) 0.123 * 100 (for the second decimal place) = 12.3
3) temp_int = int(12.3) = 12
3a) temp_float = temp_int / 100
4) int(input_value) (3)
5) return (input_value + temp_float) = 3.12

Get the idea?

[This message has been edited by silence (edited 01-01-2003).]

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana, USA
Insane since: Aug 2000

posted posted 01-01-2003 22:54

I think the ways silence suggested are the best way to do it. You probably want to use unsigned long instead of int and test to make sure the original float number is not larger than (2 ** 32) - 1.

You could also use sprintf() to translate the number to a string, operate on the string, and use sscanf() to translate it back to a float. This would be slower, but would not have the inherent size limitation of using an integer value.

Do not cast the float to a string. This will cause a segmentation fault when you try to work with the number, and will not give you the answer you want. Casting does not translate the binary form of a value. It forces the compiler to use the value as it is without complaining.




-- not necessarily stoned... just beautiful.

silence
Maniac (V) Inmate

From: soon to be "the land down under"
Insane since: Jan 2001

posted posted 01-02-2003 04:55

Yeah, type casting a float to a string is bad news.

Hyperbole had some good suggestions. However, if you can pretty much gaurantee that your data will contain numbers no larger than 2 ** 32, then go ahead and use int.

reitsma
Maniac (V) Mad Scientist

From: the bigger bedroom
Insane since: Oct 2000

posted posted 01-02-2003 05:04

umm.... has anyone discussed the modulo operand yet?

in javascript, it is the % sign.

it shows the remainder then one number is divided by another, for example:

13 % 5 = 3

so....

if you did

code:
decimal_part = original_number % 1


that should provide you with the rest of the number.

make sense?


[edit] well yes..... it seems bitdamaged had mentioned it. i figues someone had to have done so. [/edit]



[This message has been edited by reitsma (edited 01-02-2003).]

FlagMan
Bipolar (III) Inmate

From: Calgary Alberta Canada
Insane since: Dec 2002

posted posted 01-02-2003 05:08

I don't know how, or if you even can do this in C, but you could round the variable, then check if it is equal to the previous version, which would tell you whether or not it's decimal.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 01-02-2003 15:04

The modulo operator typically only works with integers; at least in C++.

silence
Maniac (V) Inmate

From: soon to be "the land down under"
Insane since: Jan 2001

posted posted 01-03-2003 00:54

Yeah, what Slime said.

C/C++ is a heavily typed language, so you have to be careful with your data types. Javascript, on the other hand, is not. Type casting would be the only way I can think of off the top of my head.

Oh, you can also use the floor(float); function. This will return the first integer less than the value entered.



Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 01-03-2003 20:28

oh, hell yeah--

that floor() function hit the nail right on the head

just floor the value and substract.

sweet!

thanks!


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

« BackwardsOnwards »

Show Forum Drop Down Menu