Compare two datasets - char

not sure this is the right place to ask but I have one set of "strings" (compiled from to int converted char) that I need to convert to a char array.
This is an example of the 5 integers:
13497
13621
13673
13547
13691
And this is the same set I need:
132
0
52
182
5
Is there an easy to way to convert them?
Edit: For added difficulty I can't find how the first set was made exactly.

Related

How do I create a time string from a number of seconds in APL?

I would like to turn a number-of-seconds-since midnight into an HH:MM:SS formatted string.
For example, the number 17672 should turn into the string '04:54:32'. I can do the math to get the integer components, like so:
T←17672
H←⌊T÷3600
MS←3600|T
M←⌊MS÷60
S←60|T
H M S
4 54 32
But I do not know how to join the three components of this array into a string (separated by colons), and left pad each time component with a zero. For example, I would want
6 0 8
to be '06:00:08'.
I can use either GNU APL or the online ngn-apl
In extended APL (all modern APLs) you can use
1↓∊':',¨¯2↑¨⍕¨100+24 60 60⊤T
ngn/apl has a non-conforming ⍕ so you'll need
1↓∊':',¨¯2↑¨,¨⍕¨100+24 60 60⊤T
Try it online!
You in Dyalog, GNU and ngn/apl you can make a dfn (lambda) out of it by enclosing it in braces. Try it in ngn/apl and in Dyalog APL.
Using Dyalog APL version 16.0 or higher, you can use the # operator to place colons:
{1↓':'#4 7∊⍕¨100+24 60 60⊤⍵}
Try it online!
The first step is to improve the calculation by using the "encode"-function:
24 60 60⊤17672
4 54 32
To format these numbers and insert the colon, I typically use ⎕FMT which is a vendor-specific function to format numbers as strings. A general way to do this in most APLs might be this:
a←,⍕3 1⍴24 60 60⊤17672
1↓,':',3 2⍴('0',a)[1+(⍳⍴a)×a≠' ']
04:54:32
Finally, instead of executing this in the session, you could put it into a function:
R←FormatSecs secs;a
a←,⍕3 1⍴60 60 60⊤secs
R←1↓,':',3 2⍴('0',a)[1+(⍳⍴a)×a≠' ']
Let's test it:
FormatSecs 17672
04:54:32
Task accomplished ;-)

double issue : lost precision

I have data as below:
241 97,5 143,5 17,5 21
203 90 113 8 23
I've loaded it to a hive table, and put data type as double but I've lost precision of data
For example 97,5 become 97,0
Perhaps it's a problem with the comma but I cant change my input file.
I've also tried to change the data type to string, it still the same but after I needed sum function it cast it to decimal I think so those values doesn't count at all.

error when access data from boost mapped_region

I have some trouble when visit mapped_region data.
First, I define a struct: (for stock quotes...)
struct bar{
double open,high,low,close;
size_t volume;
bar(double _open, double _high, double _low, double _close): open(_open),high(_high), close(_close), volume(_volume){}};
here is the sample.txt (I've also tried binary format) file for which I want to visit by iteration of bar type
89.26 89.47 89.25 89.47 563
89.47 89.56 89.27 89.47 284
89.46 89.56 89.26 89.33 264
using following code, I can read the character by character:
file_mapping m_file(filename,read_only);
mapped_region region(m_file,read_only);
char const* add= static_cast<char*> (region.get_address());
namely, for the first data, I would get 8 9 . 2 6, character by character using add[i]. This can be terrible workload.
So I want to convert :
bar* myaddr=(bar*)(region.get_address()), where bar is define as the above..
so that I can have access to data by using :
myaddr->open (with an offset ).
For instance, now I want to visit the 3rd number in the second line, I just need:
(myaddr+1)->high
However, the result is really wired:
e.g 1.50656e-189, or sometimes 825303072 for (myaddr+2)->volume
In fact, if I convert to any time beyond char , there would be such error...
Question: How can I visit mapped data by using myaddr-> without error?
Thanks
That looks like a text file. If you read it as a memory mapped region you get text, not doubles. That is your problem.

How to format floating point numbers into a string using Go

Using Go I'm trying to find the "best" way to format a floating point number into a string. I've looked for examples however I cannot find anything that specifically answers the questions I have. All I want to do is use the "best" method to format a floating point number into a string. The number of decimal places may vary but will be known (eg. 2 or 4 or zero).
An example of what I want to achieve is below.
Based on the example below should I use fmt.Sprintf() or strconv.FormatFloat() or something else?
And, what is the normal usage of each and differences between each?
I also don't understand the significance of using either 32 or 64 in the following which currently has 32:
strconv.FormatFloat(float64(fResult), 'f', 2, 32)
Example:
package main
import (
"fmt"
"strconv"
)
func main() {
var (
fAmt1 float32 = 999.99
fAmt2 float32 = 222.22
)
var fResult float32 = float32(int32(fAmt1*100) + int32(fAmt2*100)) / 100
var sResult1 string = fmt.Sprintf("%.2f", fResult)
println("Sprintf value = " + sResult1)
var sResult2 string = strconv.FormatFloat(float64(fResult), 'f', 2, 32)
println("FormatFloat value = " + sResult2)
}
Both fmt.Sprintf and strconv.FormatFloat use the same string formatting routine under the covers, so should give the same results.
If the precision that the number should be formatted to is variable, then it is probably easier to use FormatFloat, since it avoids the need to construct a format string as you would with Sprintf. If it never changes, then you could use either.
The last argument to FormatFloat controls how values are rounded. From the documentation:
It rounds the
result assuming that the original was obtained from a floating-point
value of bitSize bits (32 for float32, 64 for float64)
So if you are working with float32 values as in your sample code, then passing 32 is correct.
You will have with Go 1.12 (February 2019) and the project cespare/ryu a faster alternative to strconv:
Ryu is a Go implementation of Ryu, a fast algorithm for converting floating-point numbers to strings.
It is a fairly direct Go translation of Ulf Adams's C library.
The strconv.FormatFloat latency is bimodal because of an infrequently-taken slow path that is orders of magnitude more expensive (issue 15672).
The Ryu algorithm requires several lookup tables.
Ulf Adams's C library implements a size optimization (RYU_OPTIMIZE_SIZE) which greatly reduces the size of the float64 tables in exchange for a little more CPU cost.
For a small fraction of inputs, Ryu gives a different value than strconv does for the last digit.
This is due to a bug in strconv: issue 29491.
Go 1.12 might or might not include that new implementation directly in strconv, but if it does not, you can use this project for faster conversion.

numerical recipies ran3 generates negative numbers

I am using numerical recipes scheme to generate random numbers (ran3, page 7 in this PDF file). I didn't notice anything strange but this time, I got a negative numbers at the "warm up" stage which are larger than MBIG. The code look as if this shouldn't happen. I can easily fix this with changing the if statement to be a while statement at the line that says if(mk.lt.MZ)mk=mk+MBIG but I want to know what are the consequences.
Edit:here is the function
FUNCTION ran3a(idum)
INTEGER idum
INTEGER MBIG,MSEED,MZ
C REAL MBIG,MSEED,MZ
REAL ran3a,FAC
PARAMETER (MBIG=1000000000,MSEED=161803398,MZ=0,FAC=1./MBIG)
C PARAMETER (MBIG=4000000.,MSEED=1618033.,MZ=0.,FAC=1./MBIG)
INTEGER i,iff,ii,inext,inextp,k
INTEGER mj,mk,ma(55)
C REAL mj,mk,ma(55)
SAVE iff,inext,inextp,ma
DATA iff /0/
if(idum.lt.0.or.iff.eq.0)then
iff=1
mj=MSEED-iabs(idum)
mj=mod(mj,MBIG)
ma(55)=mj
mk=1
do 11 i=1,54
ii=mod(21*i,55)
ma(ii)=mk
mk=mj-mk
if(mk.lt.MZ)mk=mk+MBIG
mj=ma(ii)
11 continue
do 13 k=1,4
do 12 i=1,55
ma(i)=ma(i)-ma(1+mod(i+30,55))
if(ma(i).lt.MZ)ma(i)=ma(i)+MBIG
12 continue
13 continue
inext=0
inextp=31
idum=1
endif
inext=inext+1
if(inext.eq.56)inext=1
inextp=inextp+1
if(inextp.eq.56)inextp=1
mj=ma(inext)-ma(inextp)
if(mj.lt.MZ)mj=mj+MBIG
ma(inext)=mj
ran3a=mj*FAC
return
END
I was getting Seg Faults (using gfortran 4.8) because the function was trying to change the input value idum from the negative number to 1. There is no reason for that line (nor anything with iff), so I deleted it and printed out the array ma at several different places and found no negative numbers in the array.
One possibility, though, is if iabs(idum) is larger than MSEED, you might have a problem with the line mj=MSEED - iabs(idum). You should protect from this by using mj=abs(MSEED-abs(idum)) like the book has written.
Had a look at the pdf. What you need to do is
1) Seed it: value = ran3(-1)
2) Use it: value = ran3(0)

Resources