473,594 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

set double array to 0.0

Hi ,

Is there a very efficient way to set a double array to 0 ?
(I have tried memset, but the result doesn't look correct.)

Carson
Nov 14 '05 #1
32 2141
Carson wrote:
Hi ,

Is there a very efficient way to set a double array to 0 ?
(I have tried memset, but the result doesn't look correct.)


Um try something along these lines

double myarray[SIZE], tmp;

tmp = 0.0;
memcpy(myarray, &tmp, sizeof(double)) ;
memcpy(myarray+ 1, &tmp, sizeof(double)) ;
memcpy(myarray+ 2, myarray, sizeof(double)* 2);
memcpy(myarray+ 4, myarray, sizeof(double)* 4);
memcpy(myarray+ 8, myarray, sizeof(double)* 8);
memcpy(myarray+ 16, myarray, sizeof(double)* 16);
.... until you cover SIZE

Tom

Nov 14 '05 #2
In article <Gw************ ***@newssvr14.n ews.prodigy.com >,
"Carson" <ca****@ieee.or g> wrote:
Hi ,

Is there a very efficient way to set a double array to 0 ?
(I have tried memset, but the result doesn't look correct.)


Use a "for" loop. Works every time.
Nov 14 '05 #3
Carson wrote:
Hi ,

Is there a very efficient way to set a double array to 0 ?
(I have tried memset, but the result doesn't look correct.)

Carson

In my computer that's correct!
Nov 14 '05 #4
"Carson" <ca****@ieee.or g> wrote:
# Hi ,
#
# Is there a very efficient way to set a double array to 0 ?
# (I have tried memset, but the result doesn't look correct.)

Most CPUs nowadays allow all zero bits as a real zero. Did you give memset
the right length?
double r[N];
memset(r,0,N*si zeof(double));

There's a trick you can play with some implementations of memcpy.

Otherwise you have to set with some kind loop.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
A bunch of savages in this town.
Nov 14 '05 #5
Carson wrote:

Is there a very efficient way to set a double array to 0 ?
(I have tried memset, but the result doesn't look correct.)


No, not and have the results meet the standards. Tou need
something like:

for (i = 0; i < size; i++) array[i] = 0.0;

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #6
On Sun, 15 Aug 2004 22:33:10 GMT, "Carson" <ca****@ieee.or g> wrote in
comp.lang.c:
Hi ,

Is there a very efficient way to set a double array to 0 ?
(I have tried memset, but the result doesn't look correct.)

Carson


If you are defining the array, you can merely do this:

double d [SIZE] = { 0 };

....and this will initialize each and every member to 0.0.

On the other hand, if you have a malloc'ed array, or you have an array
that has unknown data in it that needs to be cleared to 0.0, a for
loop is probably best if the array is small. If it is very large,
this might be faster:

d [0] = 0; /* or 0.0, if you prefer */
memmove(d + 1, d, sizeof(double) * (SIZE - 1));

Using memmove() instead of memcpy() guarantees defined behavior even
though the source and destination areas overlap.

You would need to run timing tests on your particular
compiler/hardware combination to find out at what array size, if any,
the memmove() version becomes faster and by how much.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #7
"Carson" <ca****@ieee.or g> writes:
Is there a very efficient way to set a double array to 0 ?
(I have tried memset, but the result doesn't look correct.)


If you called memset with the right arguments, I would expect you to
see 0.0 in all entries on almost all systems. (Did you remember to
multiply the length by sizeof(double)? ) But the standard doesn't
guarantee that 0.0 is represented as all-bits-zero, so using memset
isn't strictly portable.

Probably the best solution is the simplest:

for (i = 0; i < LEN; i ++) {
arr[i] = 0.0;
}

See whether that's fast enough before you try something more
complicated.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
Jack Klein <ja*******@spam cop.net> wrote:
d [0] = 0; /* or 0.0, if you prefer */
memmove(d + 1, d, sizeof(double) * (SIZE - 1));


IMHO it won't work, unless I'm missing something very obvious.
(Values d[1], d[2]... are undetermined, and are copied
into positions d+2, d+3...)

--
Stan Tobias
sed 's/[A-Z]//g' to email
Nov 14 '05 #9
Tom St Denis wrote:
Carson wrote:

Hi ,

Is there a very efficient way to set a double array to 0 ?
(I have tried memset, but the result doesn't look correct.)

Um try something along these lines

double myarray[SIZE], tmp;

tmp = 0.0;
memcpy(myarray, &tmp, sizeof(double)) ;
memcpy(myarray+ 1, &tmp, sizeof(double)) ;
memcpy(myarray+ 2, myarray, sizeof(double)* 2);
memcpy(myarray+ 4, myarray, sizeof(double)* 4);
memcpy(myarray+ 8, myarray, sizeof(double)* 8);
memcpy(myarray+ 16, myarray, sizeof(double)* 16);
... until you cover SIZE

Tom


I believe that a simple "for" loop would be much
easier to understand and probably more efficient
{since the function call overhead is removed).

unsigned int i;
for (i = 0; i < SIZE; ++i)
{
myarray[i] = 0.0;
}
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #10

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

Similar topics

4
2772
by: Venkat | last post by:
Hi All, I need to copy strings from a single dimensional array to a double dimensional array. Here is my program. #include <stdio.h> #include <stdlib.h>
10
8643
by: Robert Palma | last post by:
I'm having trouble figuring out how to pass a pointer to a double array (1 dimensional) to a C function. Declaring array as: double xx; Declaring func. int process( double *input ) Calling func. as one of the following:
2
9345
by: antonio matos | last post by:
hi people. I have to acess a database, and there are some fields that are binary. when i acess that field is read like an array of bytes. but in reality it's an array of doubles!!! how can i convert from a byte array to a double array?
5
1764
by: mark | last post by:
I know I am being incredibly dunderheaded about this consider the following: STRICT ON Dim a(,) As Double = {{1, 2}, {3, 4}, {5, 6}} Private Sub T1(ByVal a As Array) Dim i, j As Integer For i = 0 To 2 For j = 0 To 2 a(i, j) = 2 * a(i, j) Underlined as latebinding error Next Next
3
2179
by: mark | last post by:
When I declare an array as double(,) then try to use it I get an error: "Object reference not set to an instance of an object." I have found that I can redim the array and all is well. Is my approach proper here or is the a better way for setting the instance of this specific format for an array. -- mark
0
3227
by: jim4u | last post by:
I am porting a vb library to vb.net. The vb library has an external call to an unmanaged dll. Existing code: //External function declaration Private Declare Function Uncompress& Lib "COMPR.DLL" (ByVal SrcSt$, ByVal SrcLen&, Dest As Any, ByVal DestLen&) //Invocation of external function
0
1733
by: jim4u | last post by:
I am porting a vb library to vb.net. The vb library has an external call to an unmanaged dll. Existing code: //External function declaration Private Declare Function Uncompress& Lib "COMPR.DLL" (ByVal SrcSt$, ByVal SrcLen&, Dest As Any, ByVal DestLen&) //Invocation of external function
42
5298
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
2
2167
by: anon.asdf | last post by:
Hi! Q. 1) How does one write: sizeof(array of 5 "pointers to double") ??? I know that sizeof(pointer to an array of 5 doubles) can be written as: sizeof(double (*));
3
2969
by: Michael Howes | last post by:
I have many double that each have a few thousand numbers in them. I need to concatenate groups of these double arrays into a new set of double. I don't know the total # of points. I thought it would be fairly easy to add these different double arrays to different ArrayLists and then use ToArray to get all the numbers back in one array. Sort of a cheap concatenate so I don't need to loop through all the points or keep newing larger arrays
0
7947
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7880
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8374
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8242
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5739
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3868
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1486
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.