-5

I am writing a program that works on a big matrix. Due to the lack of memory, I defined the matrix as byte[]. The problem of lack of memory was resolved, but now when I want to assign values to the elements of the matrix, it tells me that float values can not be converted to byte(the values of elements are float). So, using Convert.ToByte I converted float values to byte. The problem is that it rounds the float values and I don't want it. Is there any function that I can use and it does not round the float number? a portion of code is as follow:

static byte[,,] Score = new byte[1501,1501,501];
Score[i, j, att] = Convert.ToByte(2.0 *(Math.Log(calNum())));

where calnum is a function that calculates a float number

mary
  • 25
  • 6
  • 1
    I think something is missing there... if you need to store X floats, you cannot use an array of X bytes for it. And if you are using an array of 4X bytes, why not define it as an array of X floats? – SJuan76 Sep 30 '16 at 08:46
  • because I face the lack of memory once I am running the program, I can not define the matrix as float. so, I defined it as byte[] – mary Sep 30 '16 at 08:48
  • The downvoters presumably do not remember the time, early in their programming journey, when they first attempted to do something impossible... – AakashM Sep 30 '16 at 08:50
  • 1
    If there was any way by which you could use a `byte` to represent a `float` with the same precission, programming languages would be already using that technique instead of wasting so much memory, wouldn't they? Partition the array into chunks, keep in memory one chunk and store in disk the others, or even use a DB to store the array. – SJuan76 Sep 30 '16 at 08:51
  • @CodeCaster to me it both shows research effort and is clear (enough that I can tell what the problem is, anyway), making enough reason for an upvote. YMMV, of course. – AakashM Sep 30 '16 at 08:56
  • 1
    So ... because you don't have enough RAM to store your values in a float, you convert them to byte and wonder where the rest of your data went? – Manfred Radlwimmer Sep 30 '16 at 09:10
  • yes, but I think this is the only way I can use – mary Sep 30 '16 at 09:12
  • @mary however you encode it, one byte can only contain a limited range of numbers. If that's too limited for your use, then you're out of luck. Consider an alternative approach altogether. – CodeCaster Sep 30 '16 at 09:19
  • I do not need the numbers have high precision. just one digit after point is enough. for example 2.1 is enough. is there any way not to use float type for the matrix? – mary Sep 30 '16 at 09:28
  • Depending on the range, you could simply use a byte and divide its value by 10 while performing calculations on it, giving you a range of 0-25.5. – CodeCaster Sep 30 '16 at 09:37

2 Answers2

0

This is not possible technicaly. See here for more information: https://en.wikipedia.org/wiki/Double-precision_floating-point_format

It's actualy possible, but, well, if you want to store numbers like from -5 to 5 with some low accuracy.

If you work with math, you probably need to see in direction of sparse matrix. This matrix will optimize its internal representation with less memory impact.

eocron
  • 6,885
  • 1
  • 21
  • 50
-1

Did you try using decimal instead of float. Depending upon the number of decimals places you are trying to use, float might try to round off or even might give unreliable result. Please read further to understand difference between float, double and decimal at the below link. Hope it helps.

Difference between decimal float and double in net

Community
  • 1
  • 1
R Jain
  • 486
  • 3
  • 9
  • no, I should assign float numbers to the elements of the matrix and because I defined the matrix as byte[], it can not convert float numbers to byte. so, i used convert.tobyte() to convert float numbers. float numbers are calculated autommatically – mary Sep 30 '16 at 09:00
  • If he will use decimal he will fall into pit of even bigger memory problems. – eocron Sep 30 '16 at 10:41