Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > right truncate of float variable

Reply
Thread Tools

right truncate of float variable

 
 
Joriveek
Guest
Posts: n/a
 
      08-22-2005
I have a float variable like 123456789
I want to right truncate this and only display say only 8 right most values
all the time or otherwise filled by zeros if it is less than 8 digits, do
you know how can i display this?

Thanks
J.



 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      08-22-2005
In article <decnko$oni$(E-Mail Removed)>,
Joriveek <(E-Mail Removed)> wrote:
>I have a float variable like 123456789
>I want to right truncate this and only display say only 8 right most values
>all the time or otherwise filled by zeros if it is less than 8 digits, do
>you know how can i display this?


What do you want done if the value is (e.g.,) 3.1415926535 ?
You would only want the 15926535 displayed?
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
 
Reply With Quote
 
 
 
 
Joriveek
Guest
Posts: n/a
 
      08-22-2005
yes...only last 10 digits....

Thanx

"Walter Roberson" <(E-Mail Removed)-cnrc.gc.ca> wrote in message
news:decqnt$n8v$(E-Mail Removed)...
> In article <decnko$oni$(E-Mail Removed)>,
> Joriveek <(E-Mail Removed)> wrote:
>>I have a float variable like 123456789
>>I want to right truncate this and only display say only 8 right most
>>values
>>all the time or otherwise filled by zeros if it is less than 8 digits, do
>>you know how can i display this?

>
> What do you want done if the value is (e.g.,) 3.1415926535 ?
> You would only want the 15926535 displayed?
> --
> I was very young in those days, but I was also rather dim.
> -- Christopher Priest



 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      08-22-2005
In article <decr5h$pq2$(E-Mail Removed)>,
Joriveek <(E-Mail Removed)> top-posted:

[Note: please do not top-post: it makes conversations harder to follow]

>"Walter Roberson" <(E-Mail Removed)-cnrc.gc.ca> wrote in message
>news:decqnt$n8v$(E-Mail Removed)...
>> In article <decnko$oni$(E-Mail Removed)>,
>> Joriveek <(E-Mail Removed)> wrote:
>>>I have a float variable like 123456789
>>>I want to right truncate this and only display say only 8 right most
>>>values
>>>all the time or otherwise filled by zeros if it is less than 8 digits, do
>>>you know how can i display this?


>> What do you want done if the value is (e.g.,) 3.1415926535 ?
>> You would only want the 15926535 displayed?


>yes...only last 10 digits....


In your original posting, you said 8, and this time you said 10,
which just happens to be the number of digits after the decimal point
that I gave. What is the "rule" -- the last 8 digits before the
deciminal point if the fractional part is 0, and the first 10 digits
after the decimal point of the fractional part is non-0 ??

You do realize, I hope, that as you get into larger values
such as 123456789012345 that the representation as a float is
inexact? For example on the system I happen to be using:

(dbx) p (float) 123456789012345
123456788103168.0

Notice that the last 7 places do not agree with the original number.

Unless there is something you haven't told us about that -greatly-
restricts the values of the float, then float is not at all a good
choice for representation of numbers when particular interior positions
are more important than magnitude.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
 
Reply With Quote
 
Joriveek
Guest
Posts: n/a
 
      08-22-2005
Sorry back ground is that I will be always getting a number of float type,
always wants to extract (right most) last 10 digits (sorry it's not 8
digits). how do I do this?

I am using MSVC++ to compile the program

J.


"Walter Roberson" <(E-Mail Removed)-cnrc.gc.ca> wrote in message
news:decrkl$oa9$(E-Mail Removed)...
> In article <decr5h$pq2$(E-Mail Removed)>,
> Joriveek <(E-Mail Removed)> top-posted:
>
> [Note: please do not top-post: it makes conversations harder to follow]
>
>>"Walter Roberson" <(E-Mail Removed)-cnrc.gc.ca> wrote in message
>>news:decqnt$n8v$(E-Mail Removed)...
>>> In article <decnko$oni$(E-Mail Removed)>,
>>> Joriveek <(E-Mail Removed)> wrote:
>>>>I have a float variable like 123456789
>>>>I want to right truncate this and only display say only 8 right most
>>>>values
>>>>all the time or otherwise filled by zeros if it is less than 8 digits,
>>>>do
>>>>you know how can i display this?

>
>>> What do you want done if the value is (e.g.,) 3.1415926535 ?
>>> You would only want the 15926535 displayed?

>
>>yes...only last 10 digits....

>
> In your original posting, you said 8, and this time you said 10,
> which just happens to be the number of digits after the decimal point
> that I gave. What is the "rule" -- the last 8 digits before the
> deciminal point if the fractional part is 0, and the first 10 digits
> after the decimal point of the fractional part is non-0 ??
>
> You do realize, I hope, that as you get into larger values
> such as 123456789012345 that the representation as a float is
> inexact? For example on the system I happen to be using:
>
> (dbx) p (float) 123456789012345
> 123456788103168.0
>
> Notice that the last 7 places do not agree with the original number.
>
> Unless there is something you haven't told us about that -greatly-
> restricts the values of the float, then float is not at all a good
> choice for representation of numbers when particular interior positions
> are more important than magnitude.
> --
> "No one has the right to destroy another person's belief by
> demanding empirical evidence." -- Ann Landers



 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      08-22-2005
In article <decrr4$pvr$(E-Mail Removed)>,
Joriveek <(E-Mail Removed)> top-posted:

As I requested earlier, please don't top-post -- post your comments
*below* or interspersed with what you are commenting on. Posting your
comments at the top and then quoting the previous posting might seem
to make sense to you, but that's because the subject matter is fresh in
your mind and you know what you are trying to say. A number of us here
go through several hundred postings per day, and the only reasonable way
to keep track of what is going on is interspersed comments.

If you persist in top-posting then a number of comp.lang.c regulars will
likely instruct their newsreaders to ignore all of your posts.


>Sorry back ground is that I will be always getting a number of float type,
>always wants to extract (right most) last 10 digits (sorry it's not 8
>digits). how do I do this?


Does the decimal point count as a digit? 12345.1234 or 12345.12345 ?


>I am using MSVC++ to compile the program


Then you should be asking in an appropriate newsgroup for your
question, such as comp.lang.c++ . This is comp.lang.c, which is for C
questions, not C++ questions.

If you were using C, then I would point you in the direction of
sprintf()... but not until warning you again that receiving your
data as a float is very going to lead to grief for your purposes.
--
Feep if you love VT-52's.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      08-22-2005
Walter Roberson wrote:
> In article <decrr4$pvr$(E-Mail Removed)>,
> Joriveek <(E-Mail Removed)> top-posted:


<snip>

>>I am using MSVC++ to compile the program

>
> Then you should be asking in an appropriate newsgroup for your
> question, such as comp.lang.c++ . This is comp.lang.c, which is for C
> questions, not C++ questions.


That does *not* mean that Joriveek is using C++. I use MSVC++ regularly
to compile C code *as* C code.

> If you were using C, then I would point you in the direction of
> sprintf()... but not until warning you again that receiving your
> data as a float is very going to lead to grief for your purposes.


So give that advice.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-22-2005
"Joriveek" <(E-Mail Removed)> writes:
> Sorry back ground is that I will be always getting a number of float type,
> always wants to extract (right most) last 10 digits (sorry it's not 8
> digits). how do I do this?


What do you mean by the "last 10 digits"?

I think what you're asking for is (using the last 3 digits for
simplicity):

123456 --> 456
123.456 --> 456
0.123456 --> 456

But floating-point numbers are inexact. In a typical implementation,
123456 can be represented exactly, but the other two values cannot.
Assigning the value 123.456 to an object of type float might assign an
actual value of 123.45600128173828125; 0.123456 might give you
0.12345600128173828125.

And if you use double rather than float (which is usually a good
idea), you'll get different values.

There are no "last 10 digits" in any meaningful sense.

Can you redefine the problem? I presume getting the last 10 digits
isn't your ultimate goal. What are you trying to accomplish?

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      08-22-2005


Keith Thompson wrote:
> "Joriveek" <(E-Mail Removed)> writes:
>
>>Sorry back ground is that I will be always getting a number of float type,
>>always wants to extract (right most) last 10 digits (sorry it's not 8
>>digits). how do I do this?

>
>
> What do you mean by the "last 10 digits"?


In an episode of the comic strip "Fox Trot," the two
nerdy kids dared each other to recite the digits of pi.
Backwards.

--
(E-Mail Removed)

 
Reply With Quote
 
glen herrmannsfeldt
Guest
Posts: n/a
 
      09-27-2005
Eric Sosman wrote:

> In an episode of the comic strip "Fox Trot," the two
> nerdy kids dared each other to recite the digits of pi.
> Backwards.


In what base?

-- glen

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
truncate float to 2 decimals Junkone Ruby 5 08-14-2008 08:46 PM
How to truncate float number after dot? Marcin Tyman Ruby 7 07-26-2007 02:19 PM
Float to int conversion by using two int variables for representation of the float variable k3n3dy C++ 15 04-20-2006 06:53 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments