Hi,
I have a 32 bit integer value and I wish to find the single precision
floating point value that is closest to but less than or equal to the
integer. I also have a similar case where I need to find the single
precision floating point value that is closest to but greater than or equal
to the integer. I believe that if I simply cast to a float, it may be
assigned the next higher or lower representable value, depending on
implementation.
I am aware that if I use double precision floating point values then I
shouldn't have a problem because 32 bit integers can be represented exactly,
but I really need to use float.
Is there a simple method using standard C to achieve my goal?
Many thanks,
Jon. 8 9538
In article <bh**********@sabina.mathworks.co.uk>,
"Jonathan Fielder" <jf******@mathworks.com> wrote: Hi,
I have a 32 bit integer value and I wish to find the single precision floating point value that is closest to but less than or equal to the integer. I also have a similar case where I need to find the single precision floating point value that is closest to but greater than or equal to the integer. I believe that if I simply cast to a float, it may be assigned the next higher or lower representable value, depending on implementation.
I am aware that if I use double precision floating point values then I shouldn't have a problem because 32 bit integers can be represented exactly, but I really need to use float.
Is there a simple method using standard C to achieve my goal?
Interesting problem. I think this should give the required result on
most or all correct C implementations.
float int32_to_float_rounddown (long i) {
double d = (double) i;
double e = d;
float f;
while ((f = (float) e) > d)
e -= 1.0;
return f;
}
float int32_to_float_roundup (long i) {
double d = (double) i;
double e = d;
float f;
while ((f = (float) e)< d)
e += 1.0;
return f;
}
d and e must be double so that the conversions from 32 bit integer and
from float are exact.
Jonathan Fielder <jf******@mathworks.com> wrote: Hi,
I have a 32 bit integer value and I wish to find the single precision floating point value that is closest to but less than or equal to the integer. I also have a similar case where I need to find the single precision floating point value that is closest to but greater than or equal to the integer. I believe that if I simply cast to a float, it may be assigned the next higher or lower representable value, depending on implementation.
Will this work?
float f = myint;
if (f > (double)myint) {
f -= FLT_EPSILON * myint;
}
- Kevin.
Jonathan Fielder wrote: I have a 32 bit integer value and I wish to find the single precision floating point value that is closest to but less than or equal to the integer.
float f = myint -.25 I also have a similar case where I need to find the single precision floating point value that is closest to but greater than or equal to the integer.
float f = myint +.25 I believe that if I simply cast to a float, it may be assigned the next higher or lower representable value, depending on implementation.
Only for some of the values satisfying myint > 1/FLT_EPSILON, assuming a
sane implementation, such as any IEEE compliant one.
--
Tim Prince
In article <Ad********************@newssvr13.news.prodigy.com >,
Tim Prince <ti***********@xxxxsbcglobal.net> wrote: Jonathan Fielder wrote:
I have a 32 bit integer value and I wish to find the single precision floating point value that is closest to but less than or equal to the integer.
float f = myint -.25
Wrong result if myint = 1 I also have a similar case where I need to find the single precision floating point value that is closest to but greater than or equal to the integer. float f = myint +.25
Wrong result if myint = 1 I believe that if I simply cast to a float, it may be assigned the next higher or lower representable value, depending on implementation. Only for some of the values satisfying myint > 1/FLT_EPSILON, assuming a sane implementation, such as any IEEE compliant one.
Tim Prince <ti***********@xxxxsbcglobal.net> wrote: Jonathan Fielder wrote:
I have a 32 bit integer value and I wish to find the single precision floating point value that is closest to but less than or equal to the integer.
float f = myint -.25
If myint = 1, that gives 0.75 as f. There are many values representable
in float that are closer to 1.0 than 0.75, whilst still being less than
or equal to 1.0.
- Kevin.
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote: In article <bh**********@sabina.mathworks.co.uk>, "Jonathan Fielder" <jf******@mathworks.com> wrote:
Hi,
I have a 32 bit integer value and I wish to find the single precision floating point value that is closest to but less than or equal to the integer. I also have a similar case where I need to find the single precision floating point value that is closest to but greater than or equal to the integer. I believe that if I simply cast to a float, it may be assigned the next higher or lower representable value, depending on implementation.
I am aware that if I use double precision floating point values then I shouldn't have a problem because 32 bit integers can be represented exactly, but I really need to use float.
Is there a simple method using standard C to achieve my goal?
Interesting problem. I think this should give the required result on most or all correct C implementations.
float int32_to_float_rounddown (long i) {
double d = (double) i; double e = d; float f;
while ((f = (float) e) > d) e -= 1.0;
Why do you think that 1.0 is the smallest amount you will have to
subtract from e to make it less than i ?
- Kevin.
Kevin Easton wrote: Christian Bau <ch***********@cbau.freeserve.co.uk> wrote:"Jonathan Fielder" <jf******@mathworks.com> wrote:
....I have a 32 bit integer value and I wish to find the single precision floating point value that is closest to but less than or equal to the integer. I also have a similar case where I need to find the single precision floating point value that is closest to but greater than or equal to the integer. I believe that if I simply cast to a float, it may be assigned the next higher or lower representable value, depending on implementation.
I am aware that if I use double precision floating point values then I shouldn't have a problem because 32 bit integers can be represented exactly, but I really need to use float.
Is there a simple method using standard C to achieve my goal?
Interesting problem. I think this should give the required result on most or all correct C implementations.
float int32_to_float_rounddown (long i) {
double d = (double) i; double e = d; float f;
while ((f = (float) e) > d) e -= 1.0;
Why do you think that 1.0 is the smallest amount you will have to subtract from e to make it less than i ?
How about this?
float f = i;
double d = i;
while (f > (double)i) {
d -= 1;
f = d;
}
while (f + FLT_EPSILON != f && f + FLT_EPSILON < (double)i)
f += FLT_EPSILON;
Jirka
In article <ne********************@tomato.pcug.org.au>,
Kevin Easton <kevin@-nospam-pcug.org.au> wrote: Christian Bau <ch***********@cbau.freeserve.co.uk> wrote: In article <bh**********@sabina.mathworks.co.uk>, "Jonathan Fielder" <jf******@mathworks.com> wrote:
Hi,
I have a 32 bit integer value and I wish to find the single precision floating point value that is closest to but less than or equal to the integer. I also have a similar case where I need to find the single precision floating point value that is closest to but greater than or equal to the integer. I believe that if I simply cast to a float, it may be assigned the next higher or lower representable value, depending on implementation.
I am aware that if I use double precision floating point values then I shouldn't have a problem because 32 bit integers can be represented exactly, but I really need to use float.
Is there a simple method using standard C to achieve my goal?
Interesting problem. I think this should give the required result on most or all correct C implementations.
float int32_to_float_rounddown (long i) {
double d = (double) i; double e = d; float f;
while ((f = (float) e) > d) e -= 1.0;
Why do you think that 1.0 is the smallest amount you will have to subtract from e to make it less than i ?
This makes three assumptions: 1. All integers that fit almost into 32
bit can be stored exactly in a "double" variable. 2. Adding or
subtracting 1 to/from such a variable produces the correct result. 3.
The type float has the following property: There are two numbers fmin
and fmax such that all integers x, fmin <= x <= fmax can be represented
in a variable of type float, and no non-integer value less than fmin or
greater than fmax can be represented.
That would be the case for any simple floating point representation that
I have ever seen, and it wouldn't matter if it is binary, base 10, base
sixteen or whatever. (I know there are implementations of long double
that work differently). This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ghostdog |
last post by:
hi,
i got this opengl/c++ code:
<code>
void render(CMesh *mesh){
...
float *pVertices;
int *pIndices;
|
by: Brian Blais |
last post by:
Hello,
I saw on a couple of recent posts people saying that casting the return
value of malloc is bad, like:
d=(double *) malloc(50*sizeof(double));
why is this bad? I had always thought...
|
by: Enekajmer |
last post by:
Hi,
1 int main()
2 {
3 float a = 17.5;
4 printf("%d\n", a);
5 printf("%d\n", *(int *)&a);
6 return 0;
7 }
|
by: 6tc1 |
last post by:
Hi all, I just discovered a rounding error that occurs in C#. I'm sure
this is an old issue, but it is new to me and resulted in a fair amount
of time trying to track down the issue.
Basically...
|
by: Thorsten |
last post by:
Hi everyone,
I am rather new to C# and have a problem that will probably seem trivial to
most of you... but I hope you can still help me nevertheless..
Via the comport, I read the result of a...
|
by: redefined.horizons |
last post by:
I'm still pretty new to Python. I'm writing a function that accepts
thre integers as arguments. I need to divide the first integer by te
second integer, and get a float as a result. I don't want...
|
by: sophia.agnes |
last post by:
Hi ,
I was going through peter van der linden's book Expert C programming,
in this book there is a section named "How and why to cast"
the author then says as follows
(float) 3 - it's a...
|
by: alex.j.k2 |
last post by:
Hello all,
I have "PRECISION" defined in the preprocessor code
and it could be int, float or double, but I do not know in the
code what it is.
Now if I want to assign zero to a "PRECISION"...
|
by: Bill Reid |
last post by:
I just noticed that my "improved" version of sscanf() doesn't assign
floating point numbers properly if the variable assigned to is declared
as a "float" rather than a "double". (This never...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |