right truncate of float variable

J

Joriveek

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.
 
W

Walter Roberson

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?
 
J

Joriveek

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

Thanx

Walter Roberson said:
What do you want done if the value is (e.g.,) 3.1415926535 ?
You would only want the 15926535 displayed?
 
W

Walter Roberson

[Note: please do not top-post: it makes conversations harder to follow]
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.
 
J

Joriveek

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 said:
[Note: please do not top-post: it makes conversations harder to follow]
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.
 
W

Walter Roberson

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.
 
F

Flash Gordon

Walter said:
Joriveek <[email protected]> top-posted:


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.
 
K

Keith Thompson

Joriveek said:
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?
 
E

Eric Sosman

Keith said:
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.
 
G

glen herrmannsfeldt

Eric said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top