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

math operations

Hello Again ,

I have some problems with math operations ...

I have to sum numbers with max 4 decimals , like

12.3456
4.2
15.33
etc. etc.

and then divide them to have an average value . More numbers I have , more decimals I have in the resulting average , like
12.4567890123
How can I determine a fixed number of decimals in the result ?


Thx

David
Jan 8 '08 #1
4 2821
numberwhun
3,509 Expert Mod 2GB
Hello Again ,

I have some problems with math operations ...

I have to sum numbers with max 4 decimals , like

12.3456
4.2
15.33
etc. etc.

and then divide them to have an average value . More numbers I have , more decimals I have in the resulting average , like
12.4567890123
How can I determine a fixed number of decimals in the result ?


Thx

David
What have you tried thus far? Why not post your code and we will try and assist you from there.

Regards,

Jeff
Jan 8 '08 #2
KevinADC
4,059 Expert 2GB
It's a simple question, he can use sprintf:

Expand|Select|Wrap|Line Numbers
  1. $results = 12.4567890123;
  2. $results = sprintf ("%.4f", $results);
  3. print $results;
Jan 8 '08 #3
Thx , I'll try sprintf .
Anyway that's my code ...
Expand|Select|Wrap|Line Numbers
  1. sub Average_Value{
  2.     if (-e "$File_Dir/$Sensor_Dir.$Feature.sum.drn"){
  3.                     open (SUM , "<$File_Dir/$Sensor_Dir.$Feature.sum.drn")or die "Can't open $File_Dir/$Sensor_Dir.$Feature.sum.drn!\n ";
  4.                     $Sum=<SUM>;
  5.                     $New_Sum=$Sum+$Last_Value;
  6.                     close (SUM);
  7.                     open (SUM , ">$File_Dir/$Sensor_Dir.$Feature.sum.drn")or die "Can't open Sensor1_temp_sum.drn!\n ";
  8.                     print SUM "$New_Sum";
  9.                     close (SUM);
  10.                 }else{
  11.                 open (SUM , ">$File_Dir/$Sensor_Dir.$Feature.sum.drn");
  12.                 $New_Sum=$Last_Value;
  13.                 print SUM "$New_Sum";
  14.                 close (SUM);
  15.                 }
  16.         $Average=$New_Sum/$Count;
  17.         open (SUM , ">$File_Dir/$Sensor_Dir.$Feature.average.drn");
  18.         print SUM "$Average";
  19.         close (SUM);
  20.     return 1;
  21.     }
  22.  
  23.  

Reading the manual of sprintf there is a thing I can't understand ...
Does it just "cut" the number of decimals or does it approximate to the next closest value ?
I need something like 1.78997 -> 1.7900 , not 1.7899 .


thx again guys
Jan 8 '08 #4
I tried with your example , and seems sprint DO approximate correctly the value :)

Thx

D.
Jan 8 '08 #5

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

Similar topics

2
by: Murat Tasan | last post by:
here is the situation... i have an array... and i select something from random from it. i pick a random number x by using Math.random() and multiplying it by the length of the array. but this...
17
by: cwdjrxyz | last post by:
Javascript has a very small math function list. However there is no reason that this list can not be extended greatly. Speed is not an issue, unless you nest complicated calculations several levels...
6
by: RobG | last post by:
I am writing a script to move an absolutely positioned element on a page by a factor using style.top & style.left. The amount to move by is always some fraction, so I was tossing up between...
13
by: tings | last post by:
An article states: "In floating point maths, where if you divide by a sufficiently large number sufficiently often, you will always be able to reach a value too small to distinguish from zero,...
21
by: Rich | last post by:
I was considering C# for developing a scientific application, but I have noticed a ~30% difference between VC++ .NET and C# on the same machine, under identical conditions: double a = 0,b = 0, c...
17
by: kiplring | last post by:
float sum = (float)Math.Sqrt( floatA*floatA + floatB*floatB); I'm using DirectX with c#. But the Math class in .net framework has a problem. It is "double" base! So I'm doing type casting...
110
by: Gregory Pietsch | last post by:
I'm writing a portable implementation of the C standard library for http://www.clc-wiki.net and I was wondering if someone could check the functions in math.h for sanity/portability/whatever. I'm...
15
by: Morgan Cheng | last post by:
Hi, I am writing a program that will take a lot of Math.Cos & Math.Sin operation. I am afraid this will be source of performance impact. Anybody knows how Math.cos & Math.Sin is implemented?...
10
by: Ing. Carlos Villaseñor M. | last post by:
Hi everybody! I have developed in C# and got in a news group a math class that make matrix operations, eigenvals, eigenvecs, stat functions and much more, but now I trying to develop software in...
17
by: Albert Hopkins | last post by:
This issue may have been referred to in news:<mailman.1864.1196703799.13605.python-list@python.orgbut I didn't entirely understand the explanation. Basically I have this: 6.0 nan 6.0 nan ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.