473,386 Members | 1,720 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,386 software developers and data experts.

Multiplication of thousands of small values

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
Expand|Select|Wrap|Line Numbers
  1. long double *value;
  2. value = (long double *)malloc(n * sizeof(long double));
  3. for(i = 1...n)
  4.   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!
Jul 18 '07 #1
8 1686
JosAH
11,448 Expert 8TB
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
Jul 18 '07 #2
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:
Expand|Select|Wrap|Line Numbers
  1. long double d1, d2;
  2. fscanf(fp, "%s %lf %d", tmp, &d1, &d2);
  3. printf("%s %.30lf %d ", tmp, d1, d2);
The values printed are:
# 0.000152823802513597008798443277 0

Any idea about this?

Thank you very much!
Jul 18 '07 #3
JosAH
11,448 Expert 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. long double d1, d2;
  2. fscanf(fp, "%s %lf %d", tmp, &d1, &d2);
  3. 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
Jul 18 '07 #4
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
Expand|Select|Wrap|Line Numbers
  1. long doube d1;
  2. int d2;
which is in correct format.

Do you have any suggestion about the error?

Thanks again!
Jul 18 '07 #5
JosAH
11,448 Expert 8TB
Hi Jos,

Thank you for your reply.

Sorry that I typed wrongly in the previous post. My original code is
Expand|Select|Wrap|Line Numbers
  1. long doube d1;
  2. int d2;
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
Jul 18 '07 #6
pbmods
5,821 Expert 4TB
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?
Jul 18 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. int n = 1000;
  4. FILE* fp;
  5. fp = fopen("C:\\scratch\\instructor\\data.txt", "r");
  6. if (!fp)
  7. {
  8.    cout << "Failed to open" << endl;
  9.    return 0;
  10. }
  11. long double *value;
  12. int numread = 0;
  13. value = (long double *)malloc(n * sizeof(long double));
  14. while (fscanf(fp, "%lf", &value[numread]) != EOF)
  15. {
  16.     ++numread;
  17. }
  18. //Playback
  19. for (int i = 0; i < numread; ++i)
  20. {
  21.      printf("%e\n", value[i]);}
  22. }
  23.  
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.
Jul 18 '07 #8
ravenspoint
111 100+
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
Jul 19 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Douglas | last post by:
Gday, How would I format a number so that: TheValue = 32500 Displays in the TextBox as: $32,500.00
11
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
54
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...
9
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)
87
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...
17
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...
6
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...
5
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)...
1
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...
0
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,...
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
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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...
0
jinu1996
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...

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.