Dear all,
I need to do multiplication of thousands of floating points in C, here are some values:
4.71678396160628e-09
0.072222226257693
4.71678396160628e-09
0.0111111157230777
I need to read these values from a file, then do multiplication of them. I use - long double *value;
-
value = (long double *)malloc(n * sizeof(long double));
-
for(i = 1...n)
-
fscanf(fp, "%lf", value[i]);
However, I always get some very funny values. When I change long double to double, it also fails.
Anyone can help me?
Thank you very much!
8 1686
It could be an 'ill conditioned' system you're working with. First read all the numbers,
sort them and multiply the largest numbers first if they're all |x| < 1
kind regards,
Jos
I forgot to mention that there is error when I use the following code to read in values:
3 values to be read from the file:
# 0.000152823802513597 476
code to read and check the values: - long double d1, d2;
-
fscanf(fp, "%s %lf %d", tmp, &d1, &d2);
-
printf("%s %.30lf %d ", tmp, d1, d2);
The values printed are:
# 0.000152823802513597008798443277 0
Any idea about this?
Thank you very much!
I forgot to mention that there is error when I use the following code to read in values:
3 values to be read from the file:
# 0.000152823802513597 476
code to read and check the values: - long double d1, d2;
-
fscanf(fp, "%s %lf %d", tmp, &d1, &d2);
-
printf("%s %.30lf %d ", tmp, d1, d2);
The values printed are:
# 0.000152823802513597008798443277 0
Any idea about this?
Thank you very much!
You're trying to read an int (%d) and store the four bytes in a long double (d2).
First make sure that you can read those numbers correctly before you attempt
to multiply certain values. As far as I can see now this has nothing to do with
ill conditioned numerical systems, just IO bugs.
kind regards,
Jos
You're trying to read an int (%d) and store the four bytes in a long double (d2).
First make sure that you can read those numbers correctly before you attempt
to multiply certain values. As far as I can see now this has nothing to do with
ill conditioned numerical systems, just IO bugs.
kind regards,
Jos
Hi Jos,
Thank you for your reply.
Sorry that I typed wrongly in the previous post. My original code is
which is in correct format.
Do you have any suggestion about the error?
Thanks again!
Hi Jos,
Thank you for your reply.
Sorry that I typed wrongly in the previous post. My original code is
which is in correct format.
Do you have any suggestion about the error?
Nope, I'm sorry; after my first answer you come up with something completely
different; I answer again and then you twist the problem to something else
again. I can't follow you anymore. Please make up your mind first, state the
problem you're really experiencing and only *then* raise a question.
The way this thread works out now is not the way questions are stated and
possibly answered. You're just playing hide an' seek and I don't play those games.
kind regards,
Jos
Heya, SoFaraway.
What code are you using right now (copy and paste are your friends ~_^)?
What is an example of an input file that your script is supposed to read?
What is the output that you are getting when you run this file through your code?
OK. Let's start again:
This data:
4.71678396160628e-09
0.072222226257693
4.71678396160628e-09
0.0111111157230777
was put into a text file and that file was read by this code: -
int main()
-
{
-
int n = 1000;
-
FILE* fp;
-
fp = fopen("C:\\scratch\\instructor\\data.txt", "r");
-
if (!fp)
-
{
-
cout << "Failed to open" << endl;
-
return 0;
-
}
-
long double *value;
-
int numread = 0;
-
value = (long double *)malloc(n * sizeof(long double));
-
while (fscanf(fp, "%lf", &value[numread]) != EOF)
-
{
-
++numread;
-
}
-
//Playback
-
for (int i = 0; i < numread; ++i)
-
{
-
printf("%e\n", value[i]);}
-
}
-
These are the results:
4.71678e-009
0.0722222
4.71678e-009
0.0111111
You can't reall fscanf() into a long double since the %f means to scan to a float (6 significant figures). The excess is rounded. The %lf really does nothing more than %f and is Microsoft specific.
There were too many errors in the sample to address each one.
The trick is to specify the precision you want on output.
If you use
printf("%.20e\n", value[i]);
you should get
4.71678396160628030000e-009
7.22222262576930060000e-002
4.71678396160628030000e-009
1.11111157230777000000e-002
James
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Douglas |
last post by:
Gday,
How would I format a number so that:
TheValue = 32500
Displays in the TextBox as: $32,500.00
|
by: Soeren Sonnenburg |
last post by:
Hi all,
Just having started with python, I feel that simple array operations '*'
and '+' don't do multiplication/addition but instead extend/join an
array:
a=
>>> b=
>>> a+b
|
by: Andy |
last post by:
Hi,
I don't know if this is the correct group to post this, but
when I multiply a huge floating point value by a really
small (non-zero) floating point value, I get 0 (zero) for the result.
This...
|
by: Ralf Hildebrandt |
last post by:
Hi all!
First of all: I am a C-newbie.
I have noticed a "strange" behavior with the standart integer
multiplication. The code is:
void main(void)
|
by: Vijay Kumar R Zanvar |
last post by:
Hi,
Why multiplication of pointers is not allowed?
Till now I only know this, but not the reason why!
PS: As a rule, I searched the FAQ, but could not
find an answer.
--
Vijay Kumar R...
|
by: Christopher Dyken |
last post by:
Hi group,
I'm trying to implement two routines to handle 32x32-bits and
64x64-bits signed integer multiplication on a 32 bits machine in C. It
easy to find descriptions of non-signed...
|
by: DKode |
last post by:
I am re-creating an application that is outdated and originally created
in C++
This app, reads values in, creates objects for each line and populates
fields, then runs some conditions on the...
|
by: Paul McGuire |
last post by:
Does Python's run-time do any optimization of multiplication
operations, like it does for boolean short-cutting? That is, for a
product a*b, is there any shortcutting of (potentially expensive)...
|
by: hello12 |
last post by:
hello,
i have 2 upper triangular matrices A and B.. the values are stored in efficient format in the text file. I wanted to use those values for matrix multiplication and display the result C...
|
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: 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: 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...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |