sin() cos() 록업 테이블 연산관련 질문입니다.

epilnature의 이미지

안녕하세요.
현재 삼각함수를 모바일에서 이용하기 위해 룩업테이블 소스를 구해서
사용해보려고 하는데 소스 내용이 이해가 안되서 질문드립니다.
기존 소스를 mfc 에서 컴파일되게 변경하면서 고정소수점 관련 부분은 제거했습니다.

아래는 소스입니다.
>>>>>>>>>>>>>>>>>>>>>>Trig.h
#ifndef __ATRIG_H
#define __ATRIG_H

const int TRIG_TABLE_SIZE = 4096;

class AstTrigTable
{
private:
int data[TRIG_TABLE_SIZE];

public:
AstTrigTable();

static unsigned short ConvertDegree( unsigned short angle )
{
return ( unsigned short ) ( ( ( ( long ) angle ) << 16 ) / 3600L );
}

int Cos( unsigned short angle )
{
return ( data[ ( ( ( unsigned short ) ( angle + 0x4000 ) ) >> 4 ) ] );
}

int Sin( unsigned short angle )
{
return ( data[ angle >> 4 ] );
}
};

#endif

>>>>>>>>>>>>>>>>>>>>>>Trig.cpp
#include "stdafx.h"
#include "Trig.h"
#include "math.h"

AstTrigTable *trig_table;

#define M_PI 3.141592

/*
** AstTrigTable::AstTrigTable( void )
**
** Builds the fixed point trigonometric table.
*/
AstTrigTable::AstTrigTable()
{
int i;

for ( i = 0; i < TRIG_TABLE_SIZE/2; i++ ) {
double radian = ( ( double ) i ) * ( 2.0 * M_PI ) / ( double ) TRIG_TABLE_SIZE;
float result = sin( radian );
data[i] = result * 65536.0;
}
for ( i = 0; i < TRIG_TABLE_SIZE/2; i++ ) {
data[i+TRIG_TABLE_SIZE/2] = data[i] * -1;
}
}

우선 테이블을 만드는 부분에서 sin 함수를 사용하는데 그 안에 값이 소수점 값입니다.
호도법이라서 그런거같긴한데 저런식의 계산이 제대로 된건지 분간이 잘 안됩니다.

그리고 실제 값을 구하는 부분에서
int sinValue = trig->Sin(trig->ConvertDegree(350));
이런식으로 사용하던데 실제 350 값을 적용해보면
((long)350 << 16 / 3600L) = 6371
6371 >> 4 = 398
이 나옵니다.
실제 테이블 사이즈가 4096 인데 350 도에서 398 값이 나오는게 좀 이상합니다.
책에 있는 소스인데 잘못됬을리는 없겠고... 책에 내용에만 특화된 함수인지..
영 감이 안잡히내요.
그럼 답변 부탁드립니다.

dg의 이미지

ConvertDegree() 에서 3600으로 나눈거보니 도 단위로 나타낸 각도에 10을 곱한값을 넣어야 될거 같네요.
즉 sin 350도를 구하고 싶은면 int sinValue = trig->Sin(trig->ConvertDegree(3500));

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.