Solved

store 2 sets of numbers in 1 integer using fortran

Posted on 2004-04-27
8
225 Views
Last Modified: 2013-11-08
The programming language is fortran. Need to store 2 sets of numbers in 1 integer. the range of numbers is firstset=any number between 1 and 32-- secondset=any number between 1 and 999. The original code read:
   integer=firstset*1000+secondset
and retrived as:
   aa=integer\1000
   bb=mod(integer/1000)
 
I need to increase the first set to a maximum of 62. Is this even possible?
 
Please Help.
0
Comment
Question by:kphillips1
  • 4
  • 2
  • 2
8 Comments
 
LVL 11

Expert Comment

by:bcladd
ID: 10933503
(1)  Depends on the flavor of Fortran. Or, rather, on the size of an integer.  If it is bigger than 16 bits then your code should work as is. If you're limited to 16 bits, then things get interesting.

How about biasing your first set number by -31 so you stay on the legal range:

  integer = (firstset - 31) * 1000 + secondset

  firstset = (integer \ 1000) + 31
  secondset = integer - (firstset - 31) * 1000

mod may well work for the second calculation but I don't recall how Fortran's mod works with negative numbers.

Hope this helps,
-bcl
0
 

Author Comment

by:kphillips1
ID: 10933990
bcl
 
That's what I came up with also. Can you think of any solution without going negative. When I go negative it messes up a sort later in the program.
 
Thanks for your time
 
 
kp
0
 
LVL 11

Expert Comment

by:bcladd
ID: 10934448
I am rusty on my Fortran types but I think if you are limited to a 2 byte signed integer you are stuck with something like this.  You need 10 bits to represent 1000 values and you need 6 bits to represent 62 values. You are using all 16 bits and if the representation is signed, about half of those represent negative values.

The sort should work as it does now except (possibly, I am thinking) when the firstset values are identical. Right now 1001 sorts before 10002 and after we have -29999 which sorts before -29998 so I am pretty sure the values all sort in the same order they did before (benefit of biased notation).

What is the problem with the sort?
0
 

Expert Comment

by:AMammenT
ID: 10935793
Interesting,

bcladd hits on your solution without realizing it wasn't suggested before:

I don't know fortran, but in C:

  modFirst = firstset - 31;

  sign = modFirst < 0 ? -1 : 1;
  modFirst = sign * modFirst;

  integer = sign * (modFirst * 1000 + secondset);

and in reverse:

  sign = integer < 0 ? -1 : 1;
  integer = sign * integer;

  firstset = sign * (integer \ 1000) + 31;
  secondset = integer % 1000;




 
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Expert Comment

by:AMammenT
ID: 10935797
The sorting, of course, will now work as bcladd noted.
0
 
LVL 11

Expert Comment

by:bcladd
ID: 10937740
AMammenT -

I am not sure but I think your solution will sort incorectly. If you think about it for a second, increasing values of secondset always move your values away from 0. That is negative values get more negative for equal values of firstset as secondset values increase and positive values (for equal values of firstset) get more positive. So an incresing sort will not sort negative values by firstset and then secondset (nor will a decreasing sort handle positive values). When the failed sort was mentioned I immeadiately jumped to your suggestion as the solution but then when I looked at the ordering I found that I had happened upon an order preserving transformation.

-bcl
0
 

Author Comment

by:kphillips1
ID: 10938726
Good Morning Gentleman
 
The integer in question is written to a temp file that contains various information. That temp file is sorted and thats where I lose it. I do not know  C so I cannot convert Amamment code to fortran to test it.
 
Can u please give the fortran equivalent?
 
Thanks again.
0
 
LVL 11

Accepted Solution

by:
bcladd earned 135 total points
ID: 10938898
 integer = (firstset - 31) * 1000
  if (integer < 0)
    integer = integer - secondset
  else
    integer = integer + secondset

Always moving away from 0 so if the thousands is negative, subtract the second set, otherwise add it. So 1,999 becomes -30999 rather than -29001 (as with earlier code) and 61, 999 is 30999 in both cases


  firstset = (integer \ 1000) + 31
  secondset = integer - (firstset - 31) * 1000
  if (secondset < 0)
    secondset = -secondset

Just need to reverse the sign of the remaining part. Note that this method might work better with mod (what is -30999 mod 1000? -999 or 1? I just don't have a Fortran standard to look that one up).

It is the space and the minus, right? The sort in the file is by character line and space (0x20) sorts before minus (0x2d). You could try reversing firstset. Instead of using firstset - 31, use -firstset + 31 so the high numbers are negative and they will sort before the positive numbers. The data in the file will be all but unreadable for humanbeings but you should be able to get the sort to come out right.

-bcl
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 55
word0 challenge 4 53
Magic Software info 18 103
firstChar challenge 13 84
A short article about problems I had with the new location API and permissions in Marshmallow
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now