Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Representation of irrational numbers in DB

Author
Message
Attila
FPSC Reloaded TGC Backer
21
Years of Service
User Offline
Joined: 17th Aug 2004
Location:
Posted: 19th Apr 2010 23:38 Edited at: 19th Apr 2010 23:45
When you look at your pocket calculator and it is from the 'scientific' type you will find a key for π (pi). π is a constant whose value is the ratio of any circle's circumference to its diameter. Pi is needed to do some geometric calculations.

Pi is an irrational number, which means that its value cannot be expressed exactly as a fraction of m/n (where m and n are integers). Because pi written as decimal is a never ending number, it's 'tail' can also be used to create an endless count of numbers. Some systems use pi to create pseudo random numbers used for games or in cryptography.
The same can be said from sqrt(2) and any sqrt((m+1)/m).

DB is missing a constant for π (pi) but if you are programming geometric formulas in programs, or you are using cryptographic algorithms pi can be a handy thing to use.

There are two ways to approach the problem of getting a representation of pi, you can use a constant



or a calculation



Using the constant will be good enough for most geometric calculations in a computer, because the mathematical accuracy of a processor is limited and even if you use more precise representations of pi the result will not become more exact.

As an example this simple program shows what DBP does



Result


as you can see converting the constant pi to a string already changes its value after the 7th decimal position. Which is caused by the internal representation of real numbers. The same happens to the 22/7 from Archimedes.

There are severalt methods to approach mathematically more accurate results.

As example
for pi you could start to code a program that will calculate a approximation. One of the simpler ways is to start with a square and to calculate the length of its borders. Then use a pentagram, then a hexagon … and so on becoming more accurate with each corner you add to the calculations.


But then how, do you store such a result in a number or a string? A string would be handy because you could easily access any position of the string.

Does anyone has an idea?
Indicium
18
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 19th Apr 2010 23:41
IanM's plugins.



Rawwrr. Sig Fail.

Attila
FPSC Reloaded TGC Backer
21
Years of Service
User Offline
Joined: 17th Aug 2004
Location:
Posted: 19th Apr 2010 23:47
@Mad Nightmare
yes, but how to access the tail-numbers behind what DB stores when converting it to a string?
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 24th Apr 2010 17:23 Edited at: 25th Apr 2010 02:48
7 digits of pi is definitely good enough for accuracy's sake. Using pi rounded to 7 digits as opposed to using pi can calculate the earth's circumference with approx. 4.4 millimeters error.

[edit]

Ah, I see that you want uses for pi with cryptography. I see...

Well... you could design your own complicated system for dealing with n digit numbers, OR... you could get a precalculated value for pi (plenty of places on the internet) and save it to a text file, then look it up (although I believe there's a problem with DBPro looking up long strings in a text file). I would do the same for any irrational number.


paul5147
20
Years of Service
User Offline
Joined: 11th Jan 2006
Location: Hot & Sunny
Posted: 26th Apr 2010 16:00 Edited at: 26th Apr 2010 16:21
You could use strings for storing and working on the numbers this way,small demo i made for someone to show how long division can be made to work,how us oldies were shown at school,only to devide integers though.

cant caluclate pi with it but you could just keep changing the integers after a few loops to get away from the repeating pattern that develops.I used it for random number generator at one stage until IanM's plugin took all the hard work out of it.
paul5147
20
Years of Service
User Offline
Joined: 11th Jan 2006
Location: Hot & Sunny
Posted: 26th Apr 2010 17:53 Edited at: 26th Apr 2010 17:56
Small encryption demo using the above method,its nothing special but it does the job providing you dont share the keys
Attila
FPSC Reloaded TGC Backer
21
Years of Service
User Offline
Joined: 17th Aug 2004
Location:
Posted: 5th May 2010 10:19 Edited at: 5th May 2010 10:20
@paul5147

this is very interesting. I thought to do the following. When you use



you will get a irrational number with a endless tail (which I will call a pad). When you use these sequences to encrypt a character and you change the value m in the formula each time you encrypt a single character of your original string you will get a code that is hardly breakable by brute force attack, because every char in the encrypted message will be based on a other encoding table. And brute force attack uses a generated codes and looks then if the decoding produces words that make a sense.

The value of m can be sequentially taken from a pad containing 'random' numbers or it can even be a wraparound of some numbers, that where defined before and the communicated by another way.

To eliminate statistical decoding (by counting the occurrences of a given character) the generated pad must be long enough to keep any used character multiple times.

But to get such along pads you need math-functions that allow to produce results with (let's say) 1024 positions after the decimal point. Then translating it to a string and to access any position in the pad.

When transmitting you could even include the numbers used to generate the pads, into the message, thus making it unnecessary to transmit the table of numbers you use to create your pads.
Dia
21
Years of Service
User Offline
Joined: 16th Jan 2005
Location:
Posted: 5th May 2010 13:39 Edited at: 5th May 2010 13:40
Quote: "When transmitting you could even include the numbers used to generate the pads"


...thereby transmitting a defacto decryption key along with the cyphertext

This is not the Sig you are looking for....
paul5147
20
Years of Service
User Offline
Joined: 11th Jan 2006
Location: Hot & Sunny
Posted: 5th May 2010 20:10
Thats the problem i kept running into Attila,there just isnt a way to get DBPro to return those extra decimal places beyond a long integer range,unless you hard code the math routine yourself and store the results in a string and manipulate them in a similar way to how i did,i dont think DBPro even calculates them any further thats if it calculates them at all,i know of some compiled basic languages that use look up tables instead to speed things up.I have a routine based around the same idea except it uses 2 64 character strings as the encryption keys but splits them into 8 character sections,and uses them in sequence to encrypt each byte of the file.If you want a copy of the code ill dig it out and post it later for you.
Attila
FPSC Reloaded TGC Backer
21
Years of Service
User Offline
Joined: 17th Aug 2004
Location:
Posted: 6th May 2010 09:32
@dia, yes and avoiding to send pads that can be intercepted on their way, which would corrupt the whole mechanism - making you although independent from public-keys (as used in PGP)

@paul5147, It's a pity with 64 Bit computers and immense numbers to work with, to get only 8 positions

Login to post a reply

Server time is: 2026-07-26 04:28:21
Your offset time is: 2026-07-26 04:28:21