473,394 Members | 2,071 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,394 software developers and data experts.

C++ Calculate wrong answer.

Dear Everybody.

I got a problem with my cpp code.
I'm trying to calculate a very simple Discrete Cosine Transform (DCT),
but my c++ code seams to calculate a wrong result.

I have just started to program c++ (3 month ago), so i'm very lost
with this problem.

I have been making a test i Visual Basic and one Borland C++, but the
results seams to be very different.

I realy hope some of you guys can see some errors i my c++ code.

/Jeppe

PS: Sorry about my english. It's my second language.
***Start of Visual Basic Result***
Buffer_Block (Start)
140 144 147 140 140 155 179 175
144 152 140 147 140 148 167 179
152 155 136 167 163 162 152 172
168 145 156 160 152 155 136 160
162 148 156 148 140 136 147 162
147 167 140 155 155 140 136 162
136 156 123 167 162 144 140 147
148 155 136 155 152 147 147 136

DCT_Block (After the DCT)
1210 -18 15 -9 23 -9 -14 -19
21 -34 26 -9 -11 11 14 7
-10 -24 -2 6 -18 3 -20 -1
-8 -5 14 -15 -8 -3 -3 8
-3 10 8 1 -11 18 18 15
4 -2 -18 8 8 -4 1 -7
9 1 -3 4 -1 -7 -1 -2
0 -8 -2 2 1 4 -6 0

Buffer_Block (After the IDCT)
140 144 147 141 140 155 179 175
144 152 140 147 140 148 167 179
152 155 136 167 163 162 152 172
168 145 156 160 152 154 136 160
162 148 156 148 140 136 147 162
147 167 140 155 155 140 136 162
136 156 123 166 162 144 140 148
148 155 136 155 152 147 147 136
***End of Visual Basic Result***

***Start of C++ Result***
Buffer_Block (Start)
140 148 155 136 155 152 147 14
144 152 140 147 140 148 167 17
152 155 136 167 163 162 152 17
168 145 156 160 152 155 136 16
162 148 156 148 140 136 147 16
147 167 140 155 155 140 136 16
136 156 123 167 162 144 140 14
148 155 136 155 152 147 147 13

DCT_Block (after the DCT)
1204 83 -79 71 -41 36 -46 1
57 -98 89 -72 47 -42 48 -5
-6 -24 0 -2 -6 -12 -11 0
-53 61 -46 34 -46 20 -20 21
3 4 14 -9 1 2 27 14
13 -13 -6 -5 22 -19 10 -8
7 4 -5 3 0 -11 1 -1
83 -79 71 -41 36 -46 1 6

Buffer_Block (After the IDCT)
147 156 160 139 162 158 155 17
147 150 145 157 140 152 163 13
163 171 144 168 178 171 173 4
169 140 161 172 147 158 127 90
175 169 166 150 159 149 170 10
151 165 147 169 153 146 131 17
149 172 134 173 178 156 159 54
156 160 139 162 158 155 171 37
***End of C++ Result***

my c++ codes goes here.

#include <stdio.h>
#include <math.h>

//General variables.
unsigned char buffer_block[7][7];
int dct_block[7][7];

//Function declarements
void disp_buffer_block();
void disp_dct_block();
double c(int number);
void init_buffer_block();

static const double pi = 3.141593;

void main() {
//Initilize buffer block.
init_buffer_block();

disp_buffer_block();

for(int u=0;u<8;u++) {
for(int v=0;v<8;v++) {
double temp = 0.0;

for(int x=0;x<8;x++) {
for(int y=0;y<8;y++) {
temp += buffer_block[x][y] * cos(pi * (2 * x + 1) * u / 16) *
cos(pi * (2 * y + 1) * v / 16);
}
}

dct_block[u][v] = temp * (c(u) / 2) * (c(v) / 2);
}
}

disp_dct_block();

for(int x=0;x<8;x++) {
for(int y=0;y<8;y++) {
double temp = 0.0;

for(int u=0;u<8;u++) {
for(int v=0;v<8;v++) {
temp += (c(u) / 2) * (c(v) / 2) * dct_block[u][v] * cos(pi * (2 *
x + 1) * u / 16) * cos(pi * (2 * y + 1) * v / 16);
}
}

buffer_block[x][y] = temp;
}
}

disp_buffer_block();
}

void disp_buffer_block() {
for(int i=0;i<8;i++) {
for(int j=0;j<8;j++) {
printf("%d\t",buffer_block[j][i]);
}
printf("\n");
}
printf("\n\n");
}

void disp_dct_block() {
for(int i=0;i<8;i++) {
for(int j=0;j<8;j++) {
printf("%d\t",dct_block[j][i]);
}
printf("\n");
}
printf("\n\n");
}

double c(int number) {
if(number == 0) {
return 1 / sqrt(2);
} else {
return 1;
}
}

void init_buffer_block() {
buffer_block[0][0] = 140;
buffer_block[1][0] = 144;
buffer_block[2][0] = 147;
buffer_block[3][0] = 140;
buffer_block[4][0] = 140;
buffer_block[5][0] = 155;
buffer_block[6][0] = 179;
buffer_block[7][0] = 175;
buffer_block[0][1] = 144;
buffer_block[1][1] = 152;
buffer_block[2][1] = 140;
buffer_block[3][1] = 147;
buffer_block[4][1] = 140;
buffer_block[5][1] = 148;
buffer_block[6][1] = 167;
buffer_block[7][1] = 179;
buffer_block[0][2] = 152;
buffer_block[1][2] = 155;
buffer_block[2][2] = 136;
buffer_block[3][2] = 167;
buffer_block[4][2] = 163;
buffer_block[5][2] = 162;
buffer_block[6][2] = 152;
buffer_block[7][2] = 172;
buffer_block[0][3] = 168;
buffer_block[1][3] = 145;
buffer_block[2][3] = 156;
buffer_block[3][3] = 160;
buffer_block[4][3] = 152;
buffer_block[5][3] = 155;
buffer_block[6][3] = 136;
buffer_block[7][3] = 160;
buffer_block[0][4] = 162;
buffer_block[1][4] = 148;
buffer_block[2][4] = 156;
buffer_block[3][4] = 148;
buffer_block[4][4] = 140;
buffer_block[5][4] = 136;
buffer_block[6][4] = 147;
buffer_block[7][4] = 162;
buffer_block[0][5] = 147;
buffer_block[1][5] = 167;
buffer_block[2][5] = 140;
buffer_block[3][5] = 155;
buffer_block[4][5] = 155;
buffer_block[5][5] = 140;
buffer_block[6][5] = 136;
buffer_block[7][5] = 162;
buffer_block[0][6] = 136;
buffer_block[1][6] = 156;
buffer_block[2][6] = 123;
buffer_block[3][6] = 167;
buffer_block[4][6] = 162;
buffer_block[5][6] = 144;
buffer_block[6][6] = 140;
buffer_block[7][6] = 147;
buffer_block[0][7] = 148;
buffer_block[1][7] = 155;
buffer_block[2][7] = 136;
buffer_block[3][7] = 155;
buffer_block[4][7] = 152;
buffer_block[5][7] = 147;
buffer_block[6][7] = 147;
buffer_block[7][7] = 136;
}

my vb codes goes here.

Private Buffer_Block(7, 7) As Byte
Private DCT_Block(7, 7) As Long

Private Const PI = 3.141593

Private Sub Command1_Click()
Dim Temp As Double
Dim U As Integer, V As Integer, X As Integer, Y As Integer

Print_Buffer

For U = 0 To 7
For V = 0 To 7
Temp = 0

For X = 0 To 7
For Y = 0 To 7
Temp = Temp + (Cos(PI * (2 * X + 1) * U / 16) *
Cos(PI * (2 * Y + 1) * V / 16) * Buffer_Block(X, Y))
Next Y
Next X

DCT_Block(U, V) = Temp * (C(U) / 2) * (C(V) / 2)
Next V
Next U

Print_DCT

For X = 0 To 7
For Y = 0 To 7
Temp = 0

For U = 0 To 7
For V = 0 To 7
Temp = Temp + ((C(U) / 2) * (C(V) / 2) * Cos(PI *
(2 * X + 1) * U / 16) * Cos(PI * (2 * Y + 1) * V / 16) * DCT_Block(U,
V))
Next V
Next U

Buffer_Block(X, Y) = Temp
Next Y
Next X

Print_Buffer
End Sub

Private Function C(Num As Integer) As Double
If Num = 0 Then
C = 1 / Sqr(2)
Else
C = 1
End If
End Function

Private Function Print_Buffer()
Dim I As Integer, J As Integer, Output As String

For I = 0 To 7
Output = Buffer_Block(0, I) & vbTab

For J = 1 To 7
Output = Output & Buffer_Block(J, I) & vbTab
Next J

Print #1, Output
Next I

Print #1, vbCrLf
End Function

Private Function Print_DCT()
Dim I As Integer, J As Integer, Output As String

For I = 0 To 7
Output = DCT_Block(0, I) & vbTab

For J = 1 To 7
Output = Output & DCT_Block(J, I) & vbTab
Next J

Print #1, Output
Next I

Print #1, vbCrLf
End Function

Private Sub Form_Load()
Buffer_Block(0, 0) = 140
Buffer_Block(1, 0) = 144
Buffer_Block(2, 0) = 147
Buffer_Block(3, 0) = 140
Buffer_Block(4, 0) = 140
Buffer_Block(5, 0) = 155
Buffer_Block(6, 0) = 179
Buffer_Block(7, 0) = 175
Buffer_Block(0, 1) = 144
Buffer_Block(1, 1) = 152
Buffer_Block(2, 1) = 140
Buffer_Block(3, 1) = 147
Buffer_Block(4, 1) = 140
Buffer_Block(5, 1) = 148
Buffer_Block(6, 1) = 167
Buffer_Block(7, 1) = 179
Buffer_Block(0, 2) = 152
Buffer_Block(1, 2) = 155
Buffer_Block(2, 2) = 136
Buffer_Block(3, 2) = 167
Buffer_Block(4, 2) = 163
Buffer_Block(5, 2) = 162
Buffer_Block(6, 2) = 152
Buffer_Block(7, 2) = 172
Buffer_Block(0, 3) = 168
Buffer_Block(1, 3) = 145
Buffer_Block(2, 3) = 156
Buffer_Block(3, 3) = 160
Buffer_Block(4, 3) = 152
Buffer_Block(5, 3) = 155
Buffer_Block(6, 3) = 136
Buffer_Block(7, 3) = 160
Buffer_Block(0, 4) = 162
Buffer_Block(1, 4) = 148
Buffer_Block(2, 4) = 156
Buffer_Block(3, 4) = 148
Buffer_Block(4, 4) = 140
Buffer_Block(5, 4) = 136
Buffer_Block(6, 4) = 147
Buffer_Block(7, 4) = 162
Buffer_Block(0, 5) = 147
Buffer_Block(1, 5) = 167
Buffer_Block(2, 5) = 140
Buffer_Block(3, 5) = 155
Buffer_Block(4, 5) = 155
Buffer_Block(5, 5) = 140
Buffer_Block(6, 5) = 136
Buffer_Block(7, 5) = 162
Buffer_Block(0, 6) = 136
Buffer_Block(1, 6) = 156
Buffer_Block(2, 6) = 123
Buffer_Block(3, 6) = 167
Buffer_Block(4, 6) = 162
Buffer_Block(5, 6) = 144
Buffer_Block(6, 6) = 140
Buffer_Block(7, 6) = 147
Buffer_Block(0, 7) = 148
Buffer_Block(1, 7) = 155
Buffer_Block(2, 7) = 136
Buffer_Block(3, 7) = 155
Buffer_Block(4, 7) = 152
Buffer_Block(5, 7) = 147
Buffer_Block(6, 7) = 147
Buffer_Block(7, 7) = 136

Open "C:\TestOut.txt" For Output As #1
End Sub

Private Sub Form_Unload(Cancel As Integer)
Close #1
End Sub
Jul 22 '05 #1
5 2653
"Jepsensen" <je*******@hotmail.com> wrote...
Dear Everybody.

I got a problem with my cpp code.
[...]
my c++ codes goes here.

#include <stdio.h>
#include <math.h>

//General variables.
unsigned char buffer_block[7][7];
int dct_block[7][7];
If you need your C++ arrays to contain 8x8 matrices, you need to
declare them that, [8][8], not [7][7]. In C++ array declarations
contain the number of elements, not the highest index.
[...]

Jul 22 '05 #2
Using GCC 3.2 your C++ program produces nearly-correct output. There
still appears to be a precision issue (input differs from output by
+/- 1 in many cases), but I'm not seeing the total failure you posted
in your original message.

140 148 155 136 155 152 147 147
144 152 140 147 140 148 167 179
152 155 136 167 163 162 152 172
168 145 156 160 152 155 136 160
162 148 156 148 140 136 147 162
147 167 140 155 155 140 136 162
136 156 123 167 162 144 140 147
148 155 136 155 152 147 147 136

1204 -7 6 -6 24 -15 -11 -16
13 -19 14 -5 -9 2 17 10
-16 -9 -13 9 -17 -4 -16 2
-13 7 4 -11 -7 -9 0 11
-8 20 0 4 -10 11 21 17
0 6 -24 10 8 -8 3 -5
6 6 -7 5 0 -10 0 0
-7 6 -6 24 -15 -11 -16 0

140 149 154 134 155 152 146 145
143 146 142 151 139 145 166 183
151 161 131 159 164 164 151 162
167 136 160 166 149 150 136 170
161 156 150 140 142 140 146 151
147 158 144 161 153 136 137 170
135 160 121 162 162 146 140 140
149 154 134 155 152 146 145 138

Jul 22 '05 #3
"Derek" wrote
Using GCC 3.2 your C++ program produces nearly-correct output. There still appears to be a precision issue (input differs from output by
+/- 1 in many cases), but I'm not seeing the total failure you posted in your original message.


Sorry, I posted the wrong output. I meant to post the run with
Victor's change, which I noticed and made as well:

140 144 147 140 140 155 179 175
144 152 140 147 140 148 167 179
152 155 136 167 163 162 152 172
168 145 156 160 152 155 136 160
162 148 156 148 140 136 147 162
147 167 140 155 155 140 136 162
136 156 123 167 162 144 140 147
148 155 136 155 152 147 147 136
1209 -17 14 -8 23 -9 -13 -18
20 -34 26 -9 -10 10 13 6
-10 -23 -1 6 -18 3 -20 0
-8 -5 14 -14 -8 -2 -3 8
-3 9 7 1 -10 17 18 15
3 -2 -18 8 8 -3 0 -6
8 0 -2 3 -1 -7 -1 -1
0 -7 -2 1 1 4 -6 0
140 143 146 139 140 154 178 174
144 152 139 147 140 149 166 178
151 154 136 166 162 161 151 171
167 144 155 160 152 154 135 159
161 147 155 148 140 137 145 161
147 165 140 154 154 140 136 161
136 155 124 166 161 143 140 145
148 154 137 154 152 147 146 136
Jul 22 '05 #4
je*******@hotmail.com (Jepsensen) wrote in message news:<1b**************************@posting.google. com>...
Dear Everybody.

I got a problem with my cpp code.
I'm trying to calculate a very simple Discrete Cosine Transform (DCT),
but my c++ code seams to calculate a wrong result.

(snipped for brevity)

Your main problem is that in C++, when you divide an integer by an integer,
it give an int result (so u / 16 == 0 if u < 16) whereas VB will map this
result to floating point. Use a floating point value or just do the divide
as u / 16.0 to force this to floating point in C++.

I think another poster mentioned the fact that C++ arrays start their
indexing at 0 instead of 1; consider this also.

-- Dr. Bill Stockwell
Computer Science Department
University of Central Oklahoma
Jul 22 '05 #5

"Bill Stockwell" <bs********@ucok.edu> wrote in message news:16**************************@posting.google.c om...
Your main problem is that in C++, when you divide an integer by an integer,
it give an int result (so u / 16 == 0 if u < 16)


That's true, but the multiplicative operators associate left to right, and since his left
most operand is of type double (as are some of the intervening operands) the u,
and the 16 both get promoted to double (regardless of the order the compiler actually
computes them).

Jul 22 '05 #6

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

Similar topics

53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
6
by: charliewest | last post by:
Can someone pls point me to or recommend the easiest way to calculate someone´s age using the TimeSpan object, in .NET CF? Isn´t there a simple way to use the TimeSpan object to calculate the...
96
by: david ullua | last post by:
I am reading "Joel on Software" these days, and am in stuck with the question of "how to calculate bitsize of a byte" which is listed as one of the basic interview questions in Joel's book. Anyone...
8
by: Shilpa | last post by:
Hi Can anyone guide me by providing me the code ,for the following requirement: how to write a macro for a 16 bit processor that returns the calculated seconds of a year assuming 365 days in a...
4
by: ricky22 | last post by:
How to calculate the mean value of two numerical values? This is what i have so far. #include<stdio.h> #include<string.h> int main(void); set mean {
5
by: Michael | last post by:
Hi. I need dinamically calculate input text field based on parent static TD before showing content of input. Please, advice. Michael
5
by: Beemer Biker | last post by:
I cant seem to get that date into any DateTime to make my calculation directly by subtracting "01-01-0000" from "now". After reading this:...
2
by: Lancelot | last post by:
Hello everyone. This is my first post here, but I've been looking for answer many time. I have a crazy idea in my head and since I am quite a newby to python, I am not sure where to start or if...
15
by: student4lifer | last post by:
Hello, I have 2 time fields dynamically generated in format "m/d/y H:m". Could someone show me a good function to calculate the time interval difference in minutes? I played with strtotime() but...
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: 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
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
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,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.