473,378 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

double or float?


According to the docs, floats are 32 bit and doubles are 64 bit. So
using floats should be faster than using doubles on a 32 bit processor,
and my tests confirm this. However, most of the Math methods deal with
doubles and I'm having problems with casting them back to floats.

For example:
(double)0.1f = 0.10000000149011612

I don't need the accuracy of doubles, but the speed of floats would be
benificial, but it seems I'm either going to have to write my own math
functions or just use doubles. For the record my rather basic test,
which just performed lots of multiplications and divisions, took half
the time using floats as using doubles.

Has anyone else had this problem? And can someone give me an efficient
Floor algorithm so I can write my own floating point version?

James.

Nov 15 '05 #1
6 7234

Michael Mayer wrote:

I guess I'm curious why you can't cast back to floats? Do you need the
precision or not? I have a feeling you're running into this problem:

http://www.pobox.com/~skeet/csharp/floatingpoint.html
(thanks to Jon Skeet for the article)


I don't need double precision, but the inaccuracy caused by casting can
make, for example, the Floor function give the incorrect answer:

float min=1.0f, increment=0.1f;
double result = Math.Floor(min/increment)*increment;

I'm leaving the result as a double in this example - its casting from
float to double that's causing the problem here. In theory,
min/increment will be 10.0 which is also the Floor. Multiply it by
increment and you get 1.0 again. But result comes out as
0.90000001341104507. (Try it - I swear it's true!)

Now I get that number if I put "Math.Floor(1.0f/(double)0.1f)*0.1f" into
the watch window. Even wierder, If I copy and paste
"Math.Floor(min/increment)*increment" into the watch window it gives me
the correct answer of 1.0. So I guess the compiler must be making the
conversion to double earlier than I would have expected, but either way
it's causing a problem.

I'm starting to think I should just use doubles... processors of the
future will be 64bit anyway I guess.
Nov 15 '05 #2
The code at the end demonstrates your problem. When casting from a float
number up to a double, you are getting some slightly skewed results. If you
divide 1/.1 in float or double you always get 10, however, casting a float
10 to a double results in some small amount of inaccuracy 9.99999985098839.
Generally you can work this off by using an error quotient or some small
decimal that gives a degree of freedom. This could be demonstrated by doing
(float fixedError = .0001f; Math.Floor((min/increment)+fixedError);) You
could also make use of the Round function instead of the floor method, or
you could roll your own floor method for floats.

using System;

public class Locality {
private static void Main(string[] args) {
float min=1.0f, increment=0.1f;
double mind=1.0D, incrementd=0.1D;

Console.WriteLine(min/increment);
Console.WriteLine(mind/incrementd);
Console.WriteLine((double) (min/increment));

Console.WriteLine(Math.Floor(min/increment));
Console.WriteLine(Math.Floor(mind/incrementd));

double result = Math.Floor(min/increment)*increment;
Console.WriteLine(result);
}
}
--
Justin Rogers
DigiTec Web Consultants, LLC.

"James Thurley" <ne********@jamesthurley.NO__SPAM.com> wrote in message
news:bi**********@titan.btinternet.com...

Michael Mayer wrote:

I guess I'm curious why you can't cast back to floats? Do you need the
precision or not? I have a feeling you're running into this problem:

http://www.pobox.com/~skeet/csharp/floatingpoint.html
(thanks to Jon Skeet for the article)


I don't need double precision, but the inaccuracy caused by casting can
make, for example, the Floor function give the incorrect answer:

float min=1.0f, increment=0.1f;
double result = Math.Floor(min/increment)*increment;

I'm leaving the result as a double in this example - its casting from
float to double that's causing the problem here. In theory,
min/increment will be 10.0 which is also the Floor. Multiply it by
increment and you get 1.0 again. But result comes out as
0.90000001341104507. (Try it - I swear it's true!)

Now I get that number if I put "Math.Floor(1.0f/(double)0.1f)*0.1f" into
the watch window. Even wierder, If I copy and paste
"Math.Floor(min/increment)*increment" into the watch window it gives me
the correct answer of 1.0. So I guess the compiler must be making the
conversion to double earlier than I would have expected, but either way
it's causing a problem.

I'm starting to think I should just use doubles... processors of the
future will be 64bit anyway I guess.

Nov 15 '05 #3
> I don't need double precision, but the inaccuracy caused by casting can
make, for example, the Floor function give the incorrect answer:

float min=1.0f, increment=0.1f;
double result = Math.Floor(min/increment)*increment;


There is no exact representation of 0.1 in binary.
Nov 15 '05 #4
Justin Rogers <Ju****@games4dotnet.com> wrote:
A floating point version of floor? Remember floor is doing nothing more
than storing an integer value so the (int) cast is perfect for this
operation.


Except they do different things for negative numbers - casting to int
rounds to 0, Floor always rounds down.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #5

Jon Skeet wrote:
Justin Rogers <Ju****@games4dotnet.com> wrote:
A floating point version of floor? Remember floor is doing nothing more
than storing an integer value so the (int) cast is perfect for this
operation.

Except they do different things for negative numbers - casting to int
rounds to 0, Floor always rounds down.


Hmm... I guess something like this should work:

public int Floor(float val){
if(val>0.0f || val==(int)val)
return (int)val; // Positive numbers and negative whole numbers
else
return ((int)val) - 1; // Negative floating point numbers
}

Not very pretty, but should be fast!
Nov 15 '05 #6
> I'm starting to think I should just use doubles... processors of the
future will be 64bit anyway I guess.


the "bitness" of a CPU has nothing to do with this.
On a 32 bit CPU a float can be fetched in 1 cycle and a double in 2 cycles
On a 64 bit CPU 2 floats can be fetched in 1 cycle and a double in 1 cycle

So the net effect will be the same.
The only thing that is really a factor is how well the FP units within the
CPU are implemented.

If you are really trying to crunch numbers then look at the CPU specific
SIMD instructions available for your target CPU. Oh, and forget c# since
the runtime doesn't use them.

Oscar
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Mair | last post by:
Hi there, actually, I have posted the same question in g.g.help. As there were no answers, I am still not sure whether this is a bug or only something open to the compiler that is seemingly...
22
by: bq | last post by:
Hello, Two questions related to floating point support: What C compilers for the wintel (MS Windows + x86) platform are C99 compliant as far as <math.h> and <tgmath.h> are concerned? What...
9
by: Sisyphus | last post by:
Hi, I have some software that does the following (in an attempt to determine whether the double x, can be represented just as accurately by a float): void test_it(double x) { float y = x;...
13
by: maadhuu | last post by:
hello , i would like to know as to why double is more efficient than float . thanking you, ranjan.
44
by: Daniel | last post by:
I am grappling with the idea of double.Epsilon. I have written the following test: public void FuzzyDivisionTest() { double a = 0.33333d; double b = 1d / 3d; Assert.IsFalse(a == b,...
67
by: lcw1964 | last post by:
This may be in the category of bush-league rudimentary, but I am quite perplexed on this and diligent Googling has not provided me with a clear straight answer--perhaps I don't know how to ask the...
60
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
2
by: tkirankumar | last post by:
Hi all, uname -a SunOS cbmrsd1a1 5.10 Generic_118833-17 sun4us sparc FJSV,GPUZC-M g++ -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.10/3.3.2/specs Configured with:...
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.